Types of User Defined Functions
Types of User Defined Functions
A user-defined function is one that is defined by the user when writing any program,
as we do not have library functions that have predefined definitions. To meet the
specific requirements of the user, the user has to develop his or her own functions.
Such functions must be defined properly by the user.
Syntax
Example
Output
Explanation
In the above program, function sum does not take any arguments and has no
return values. It takes x and y as inputs from the user and prints them inside the
void function.
2. Function with No Arguments and With Return Value
Functions that have no arguments but have some return values. Such functions
are used to perform specific operations and return their value.
Syntax
return_type function_name()
{
// program
return value;
}
Example
// C program to use function with no argument and with return values
#include <stdio.h>
int sum()
{
int x, y, s = 0;
printf("Enter x and y\n");
scanf("%d %d", &x, &y);
s = x + y;
return s;
}
// Driver code
int main()
{
// function call
printf("Sum of x and y is %d", sum());
return 0;
}
Output
Enter x and y
419 0
Sum of x and y is 419
Explanation
In the above program, function sum does not take any arguments and has a return
value as an integer type. It takes x and y as inputs from the user and returns them.
3. Function With Arguments and No Return Value
Functions that have arguments but no return values. Such functions are used to
display or perform some operations on given arguments.
Syntax
void function_name(type1 argument1, type2 argument2,...typeN argumentN)
{
// Program
}
Example
// C program to use function with argument and no return values
#include <stdio.h>
void sum(int x, int y)
{
printf("Sum of %d and %d is: %d", x, y, x + y);
}
// Driver code
int main()
{
int x, y;
printf("Enter x and y\n");
scanf("%d %d", &x, &y);
// function call
sum(x, y);
return 0;
}
Output
Enter x and y 0 0
Sum of 0 and 0 is: 0
Explanation
In the above program, function sum does take x and y as arguments and has no
return value. The main function takes x and y as inputs from the user and calls the
sum function to perform the print operation on the given arguments.
4. Function With Arguments and With Return Value
Functions that have arguments and some return value. These functions are used
to perform specific operations on the given arguments and return their values to
the user.
Syntax
return_type function_name(type1 argument1, type2 argument2,...typeN
argumentN)
{
// program
return value;
}
Example
// C program to use function with argument and with return values
#include <stdio.h>
int sum(int x, int y) { return x + y; }
// Driver code
int main()
{
int x, y;
printf("Enter x and y\n");
scanf("%d %d", &x, &y);
// function call
printf("Sum of %d and %d is: %d", x, y, sum(x, y));
return 0;
}
Output
Enter x and y
Sum of 0 and 0 is: 0
Explanation
In the above program, function sum takes two arguments as x and y, and has a
return value as an integer type. The main function takes input x and y from the
user and calls the sum function to perform a specific operation on the given
arguments and returns the value.