Loading...
C User-defined Function Types

C User-defined Function Types

In this tutorial, we will learn about different types of user-defined functions in C programming with the help examples.

Types of User-defined Functions

  1. Function with no arguments and no return value
  2. Function with no arguments and a return value
  3. Function with arguments and no return value
  4. Function with arguments and no return value

Below, we will discuss about all these types, along with examples. Consider a situation in which you have to check greater number. This problem is solved below by making user-defined function in 4 different ways as mentioned above.


Function with no arguments and no return value

When we make any function with no arguments and no return value, it neither receives any data from the calling function nor returns a value. In other words, we can say the calling function does not get any value from the called function.

Such functions neither take any argument nor return any value.

Such functions can either be used to display information because they are completely dependent on user inputs.
In this type of function each function is independent.

#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);
  }
}

Output :

Enter two integer : 12 14
The greater number is : 14

Here, The greatNum() function takes two integer inputs from the user, and checks which one is greater .

The empty parenthesis in greatNum(); statement inside the main() function indicates that no argument is passed to the function.

The return type of the greatNum is void. Hence, no value is returned from the function.


Function with no arguments and a return value

This type of functions, arguments are passed through the calling function to called function but the called function returns value.

Such type of function are independent.

#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;
}

Output :

Enter two integer: 12 11 
The greater number is 12

Here, The empty parenthesis in the result = greatNum(); statement indicates that no argument is passed to the function. And, the value returned from the function is assigned to result.

The greatNum() function takes two input from the user and returns it, Then check which one is greater.

The return type of the greatNum is int. Hence, it returns a value greater_number.


Function with arguments and no return value

This type of function, arguments are passed through the calling function to called function but does not return value.

Such type of function practically dependent on each other.

#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);
  }
}

Output :

Enter two integers : 45 54
The greater number is 54

The integer value entered by the user is passed to the greatNum() function .

Here, the greatNum() function checks which one is greater.

The return type of the greatNum() is void, Hence, no value is returned from the function.


Function with arguments and a return value

This is the best type, as this makes the function compeletely independent of inputs and outputs, and only the logic is defined inside the function body.

Both functions are dependent on each other.

#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;
  }
}

Output :

Enter two integer: 57 61 
The greater number is 61

The integer value entered by the user is passed to the greatNum() function .

Here, the greatNum() function checks which one is greater.

The return type of the greatNum() is int. Hence, it returns a value.


Next Tutorial

We hope that this tutorial helped you develop better understanding of the concept of Function Types in C.

Keep Learning : )

In the next tutorial, you'll learn about C Recursion.

- Related Topics