A user-defined function is a function created by the programmer to perform a specific task. It helps make programs modular, reusable, and easier to maintain.
- User-defined functions are written by the programmer based on the program's requirements.
- Unlike library functions, they do not require any predefined header file and their behavior is defined by the user.
#include <stdio.h>
// Function definition
void greet()
{
printf("Welcome to C Programming!");
}
int main()
{
// Function call
greet();
return 0;
}
Output
Welcome to C Programming!
Using User-Defined Functions in C
To use a user-defined function, we first have to understand the different parts of its syntax. The user-defined function in C can be divided into three parts:
1. C Function Prototype
A function prototype (or function declaration) specifies a function's name, return type, and parameters without including its body. It informs the compiler about the function before it is used in the program.
- Declares the function's name, return type, and parameter list.
- Enables the compiler to check function calls for correct arguments and return type.
#include <stdio.h>
// Function prototype
int add(int, int);
int main()
{
int result = add(10, 20);
printf("Sum = %d", result);
return 0;
}
// Function definition
int add(int a, int b)
{
return a + b;
}
Syntax
return_type function_name (type1 arg1, type2 arg2, ... typeN argN);
We can also skip the name of the arguments in the function prototype. So,
return_type function_name (type1 , type2 , ... typeN);

2. C Function Definition
A function definition contains the actual code that specifies what a function does. It includes the function's return type, name, parameters, and a body enclosed within curly braces { }.
- Contains the executable statements that perform the function's task.
- The function body is enclosed within { } braces.
#include <stdio.h>
// Function definition
int add(int a, int b)
{
return a + b;
}
int main()
{
int result = add(10, 20);
printf("Sum = %d", result);
return 0;
}
Output
Sum = 30
Syntax
return_type function_name (type1 arg1, type2 arg2 .... typeN argN) {
// actual statements to be executed
// return value if any
}
Note: If the function call is present after the function definition, we can skip the function prototype part and directly define the function.
3. C Function Call
A function call is used to transfer control from the calling function to a user-defined function for execution. A function is called using its name followed by parentheses (), with arguments passed inside the parentheses if required.
- Invokes the function and executes the statements defined in its body.
- Arguments passed in the function call are assigned to the function's parameters.
#include <stdio.h>
// Function definition
void greet()
{
printf("Welcome to C Programming!");
}
int main()
{
// Function call
greet();
return 0;
}
Output
Welcome to C Programming!
Syntax
function_name(arg1, arg2, ... argN);Components of Function Definition
There are three components of the function definition:
1. Function Parameters
Function Parameters (Arguments) are the values passed to a function during a function call. The number, order, and data types of the arguments should match the parameters specified in the function definition.
Syntax:
int foo (int a, int b);
Here, a and b are function parameters.
Note: C language provides a method using which we can pass variable number of arguments to the function. Such functions are called variadic function.
2. Function Body
The function body is the set of statements that are enclosed within { } braces. They are the statements that are executed when the function is called.
Syntax:
int foo (int a, int b) {
int sum = a + b;
return sum;
}
Here, the statements between { and } is function body.
3. Return Value
The return value is the value returned by the function to its caller. A function can only return a single value and it is optional. If no value is to be returned, the return type is defined as void.
The return keyword is used to return the value from a function.
Syntax:
int foo (int a, int b) {
return a + b;
}
Note: We can use pointers or structures to return multiple values from a function in C.
Passing Parameters to User-Defined Functions
We can pass parameters to a function in C using two methods:
1. Call by value
Call by Value is a parameter passing method in which a copy of the actual argument is passed to the function. Any changes made inside the function do not affect the original variable.
- The function works on a copy of the original value.
- Changes made inside the function are not reflected in the calling function.
// C program to show use of
// call by value
#include <stdio.h>
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
// Driver code
int main()
{
int x = 10, y = 20;
printf("Values of x and y before swap are: %d, %d\n", x,
y);
swap(x, y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}
Output
Values of x and y before swap are: 10, 20 Values of x and y after swap are: 10, 20
Note: Values aren't changed in the call by value since they aren't passed by reference.
2. Call by Reference
Call by Reference is a parameter passing method in which the address of the actual argument is passed to the function using pointers. Any changes made inside the function affect the original variable.
- The function works with the original variable through its address.
- Changes made inside the function are reflected in the calling function.
// C program to implement
// Call by Reference
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Driver code
int main()
{
int x = 10, y = 20;
printf("Values of x and y before swap are: %d, %d\n", x,
y);
swap(&x, &y);
printf("Values of x and y after swap are: %d, %d", x,
y);
return 0;
}
Output
Values of x and y before swap are: 10, 20 Values of x and y after swap are: 20, 10
Advantages
The advantages of using functions in the program are as follows:
- Promote code reusability by allowing the same function to be used multiple times, reducing code duplication.
- Improve readability and maintenance by dividing large programs into smaller, manageable functions.
- Simplify development by hiding implementation details and enabling functions to be reused across different programs.
Related Article: