In this guide, you will learn how to create user-defined function in C. A function is a set of statements that together perform a specific task.
If you are new to this topic, I highly recommend you to read my complete guide on functions: Functions in C Programming.
An example of function: You are frequently writing 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 inside that function, you can write these lines of code. This allows you to reuse your code and improves the readability of your code.
What is a User-defined function?
A user defined function is a function that is declared by user, which means you can declare a function with any name for a specific task:
Note: Pre-defined function: A function that is already present in the C library are known as predefined functions such as printf() scanf() are predefined or built-in functions.
Let’s take an example, where we are declaring a function to find the quotient and remainder after dividing a number by another number.
#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; }
Output:
User defined function signature
Function signature (or prototype): A function signature contains function name, its return type and parameters list. As you can see in the above program, we have mentioned the function signatures at the starting of the program. This is a way to inform the compiler that these functions are later declared in the program.
Syntax:
return_type function_name(parameters list)
In our example:
int findQuotient(int a, int b);
int
is thereturn_type
of the functionfindQuotient
is thefunction_name
(int a, int b)
is the parameter list
int findRemainder(int a, int b);
int
is thereturn_type
of the functionfindRemainder
is thefunction_name
(int a, int b)
is the parameter list
Note: If you declare the functions before the main() function then there is no need to mention the function signatures at the start of the program. As compiler would already read the function definition before it begins executing main function
Calling a user defined function
Theoretically a function call is equivalent to replacing the call by the line of code inside a function. When a function is called, the arguments that we pass while calling the function are used by function to perform the specific task and return a value.
Note: Functions with void return type doesn’t return anything, such functions are usually used to print something or calling another function.
Syntax of function call:
function_name(argument1, argument2, ...);
In the above example, we called the user defined functions like this:
quo = findQuotient(num1, num2); rem = findRemainder(num1, num2);
Since both the functions have int return type, integer variables are used to store the result.
Function definition
A function definition contains function body that includes set of statements that are executed every time this function is called. The arguments that we pass during function call are copied to the parameters a and b, these values are used for calculation and at the end a result is returned to the function call using return statement.
// function definition int findQuotient(int a, int b) { int q; //division operator to find quotient q = a/b; return q; }
Note: When a function is called, control is transferred to the called function and when a return statement is encountered inside a function, control is transferred back to the where the function is called.
How arguments are used by the function
As discussed earlier, the arguments are passed to the function parameters and used by statements inside function body. The important point to note here is that the data type of the parameters and the passed arguments must be same. For example, you cannot pass float arguments while calling a function that has int parameters.
Also, the sequence of data types must match. For example: a function sum(int a, float b)
cannot be called like this:
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
The valid call for this function is:
sum(5, 5.5);
Return statement in user defined function
The data type of the return value and the variable that is storing the result of function call must match. In our example, both the user defined functions are returning int values and int variables quo
and rem
are used to store these results.