C

Examples Of User-Defined Functions In C – Most Detailed

This guide will teach you how to write a user-defined function in C. A function is a collection of statements that work together to complete a specific task.
If you are new to this topic, I strongly advise you to read my comprehensive guide on functions: In C programming, functions are used.
A function example: You frequently write 4-5 lines of code to find the sum of two numbers; you can easily replace these 4-5 lines with a function call and write these lines of code inside that function. This allows you to reuse your code and improves its readability.

What precisely is a user-defined function?

A user defined function is one that is declared by the user, which means that it can have any name for a specific task:
Take note of the following: Predefined functions are functions that are already present in the C library, such as printf() and scanf().
Consider the following example: we are declaring a function to find the quotient and remainder after dividing one number by another.

#include <stdio.h>
// function signature
int findQuotient(int a, int b);
int findRemainder(int a, int b);

int main()
{
  int num1, num2, quo, rem;

  printf("Enters two integer numbers: ");
  scanf("%d %d",&num1,&num2);

  // calling user defined function findQuotient
  quo = findQuotient(num1, num2);
  printf("Quotient is: %d",quo);

  // calling user defined function findRemainder
  rem = findRemainder(num1, num2);
  printf("\nRemainder is: %d",rem);

  return 0;
}

// function definition
int findQuotient(int a, int b)
{
  int q;
  //division operator to find quotient
  q = a/b;
  return q;
}
// function definition
int findRemainder(int a, int b)
{
  int r;
  //modulus operator to find the remainder
  r = a%b;
  return r;
}

Signature of a user-defined function

Signature (or prototype) of a function: A function signature includes the function name, return type, and a list of parameters. As you can see in the above program, we mentioned the function signatures at the beginning. This tells the compiler that these functions will be declared later in the program.
Syntax:

return_type function_name(parameters list)

In our example:

int findQuotient(int a, int b);
  • int is the return_type of the function
  • findQuotient is the function_name
  • (int a, int b) is the parameter list
int findRemainder(int a, int b);
  • int is the return_type of the function
  • findRemainder is the function_name
  • (int a, int b) is the parameter list

Note: It is not necessary to mention the function signatures at the start of the program if the functions are declared before the main() function. Before executing the main function, the compiler would have already read the function definition.

Making a call to a user-defined function

In theory, a function call is equivalent to replacing the call with a line of code within a function. When we call a function, the arguments we pass to the function are used by the function to perform the specific task and return a value.
Note: Functions with void return types do not return anything; they are typically used to print something or to call another function.
Function call syntax:

function_name(argument1, argument2, ...);

In the preceding example, we referred to the user-defined functions as follows:

quo = findQuotient(num1, num2);
rem = findRemainder(num1, num2);

Because both functions have int return types, the results are stored in integer variables.

Definition of a Function

A function definition includes a function body, which is a collection of statements that are executed each time the function is called. The arguments passed to the function call are copied to the parameters a and b, and these values are used for calculation before returning a result to the function call via the return statement.

// function definition
int findQuotient(int a, int b)
{
  int q;
  //division operator to find quotient
  q = a/b;
  return q;
}

When a function is called, control is transferred to the called function; when a return statement is encountered within a function, control is transferred back to the function that was called.

The function’s use of arguments

As previously discussed, arguments are passed to function parameters and used by statements within the function body. It is critical to note that the data types of the parameters and the passed arguments must be the same. For example, you cannot pass float arguments to a function with int parameters.
Furthermore, the data type sequence must be consistent. A function sum(int a, float b), for example, cannot be called as follows:

sum(15.5, 10); //first parameter is int in function definition 
or
sum(10, 20); //second parameter is float in function definition 
or
sum(10.5, 10.5) //first parameter is int in function definition

This function’s valid call is:

sum(5, 5.5);

In a user-defined function, the return statement

The data type of the return value and the variable storing the result of the function call must be the same. In our example, both user-defined functions return int values, which are stored in the int variables quo and rem.

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button