Types of user-defined functions in c; In this tutorial, You will learn different types of user-defined functions in C programming with the help of examples.
Types of User-defined Functions in C
There are 4 different types of user-defined functions in c programming, as follows:
- Function with no arguments and no return value
- Function with no arguments and a return value
- Function with arguments and no return value
- Function with arguments and no return value
Function with no arguments and no return value
See the following example for function with no arguments and no return value in c programming; as follows:
#include <stdio.h>
int main(){
greatNum(); // argument is not passed
return 0;
}
// return type is void meaning doesn't return any value
void greatNum(){
int i, j;
printf("Enter two integer: ");
scanf("%d %d",&i ,&j);
if(i > j){
printf("The greater number is : %d", i);
}
else {
printf("The greater number is : %d", j);
}
}
The output of the above program; as follows:
Enter two integer : 12 14 The greater number is : 14
Function with no arguments and a return value
See the following example for function with no arguments and return value in c programming; as follows:
#include <stdio.h>
int main(){
int result;
result = greatNum(); //Function called and argument is not passed
printf("The greater number is %d", result);
return 0;
}
// return type is int meaning it return some value
int greatNum(){
int i, j, greater_number;
printf("Enter two integer: ");
scanf("%d %d",&i ,&j);
if(i > j){
greater_number = i;
}
else {
greater_number = j;
}
return greater_number;
}
The output of the above program; as follows:
Enter two integer: 12 11 The greater number is 12
Function with arguments and no return value
See the following example for function with arguments and no return value in c programming; as follows:
#include <stdio.h>
int main(){
int i, j;
printf("Enter two integers :");
scanf("%d %d",&i ,&j);
greatNum(i, j); //Function called and argument is passed
return 0;
}
// return type is void meaning it return no value
void greatNum(int x,int y){
if(x > y){
printf("The greater number is %d", x);
}
else {
printf("The greater number is %d", y);
}
}
The output of the above program; as follows:
Enter two integers : 45 54 The greater number is 54
Function with arguments and a return value
See the following example for function with arguments and return value in c programming; as follows:
#include <stdio.h>
int greatNum(int i, int j);
int main(){
int i, j, result;
printf("Enter two integers :");
scanf("%d %d",&i ,&j);
result = greatNum(i, j);
printf("The greater number is %d", result);
//Function called and argument is passed
return 0;
}
// return type is void meaning it return no value
void greatNum(int x, int y){
if(x > y){
return x;
}
else {
return y;
}
}
The output of the above program; as follows:
Enter two integer: 57 61 The greater number is 61