Types of functions in C

In this section, we will learn about the different categories of user-defined functions used in C based on whether they pass arguments or not and whether they return a value or not.

From now on, we will use the term arguments which we knew as parameters in the previous section more frequently.

Now let’s move on and discuss the types of user-defined functions C programming language has to offer.

Types of User-defined Functions

A user-defined function has different properties or different approach to be used in writing a program, based on which, a function is divided into four categories, namely:

  • Functions with No Arguments and No Return Value
  • Functions with No Arguments and One Return Value
  • Functions with Arguments and No Return Value
  • Functions with Arguments and One Return Value

In each of the categories, we will use a common user-defined function Add() and solve it using different approaches.

No Arguments and No Return Value

In this type, a function has no arguments, so it does not receive any value from the calling function. Similarly, it does not return a value, so the calling function does not receive any data from the called function.
In other words, there is no transfer of data between the called function and the calling function.

Below is an example to demonstrate the use of function with no arguments and no return value.

Example: Program to add two numbers using a user-defined function.

#include<stdio.h>
void Add();

int main()
{
    Add();
    return 0;
}

void Add()
{
  int n,x,y;
  printf("Enter two mumbers:");
  scanf("%d%d",&x,&y);
  n=x+y;
  printf("%d",n);

}

Output:

Enter two numbers:5
2
7

Explanation:

In line 6 of the above code, the empty parentheses of function Add() inside the function main() indicates that no arguments are passed in the function.
Similarly, in line 10, the return type of the function Add() is void which means no value is returned from the body of the function to the calling function main().

No Arguments and One Return Value

In this type, a function has no arguments, so it does not receive any value from the calling function. But, it has a return value, so the calling function receives a value from the called function.

Below is an example to demonstrate the use of function with no arguments and one return value.

Example: Program to add two numbers using a user-defined function.

#include<stdio.h>
int Add();

int main()
{
  int Sum;
  Sum = Add();
  printf("%d",Sum);
  return 0;
}
int Add()
{
  int n,x,y;
  printf("Enter two mumbers:");
  scanf("%d%d",&x,&y);
  n=x+y;
  return n; 
}

Output:

Enter two numbers:2
3
5

Explanation:

In line 7 of the above code, the empty parentheses of function Add() inside the function main() indicates that no arguments are passed in the function.
But, in line 17, function Add() returns the value of n to the calling function main().

Arguments and No Return Value

In this type, a function has arguments, so it receives values from the calling function. But, it does not return a value, so the calling function does not receive any data from the called function.

Below is an example to demonstrate the use of function with arguments and no return value.

Example: Program to add two numbers using a user-defined function.

#include<stdio.h>
void Add(int,int);
int main()
{
  int x,y;
  printf("Enter two mumbers:");
  scanf("%d%d",&x,&y);

  Add(x,y);
  return 0;
}
void Add(int x,int y)
{
  int n;
  n=x+y;
  printf("%d",n);
  
}

Output:

Enter two numbers:8
4
12

Explanation:

In line 9 of the above code, arguments x and y are passed in the function Add() inside the function main().
But, in line 12, the return type of the function Add() is void which means no value is returned from the body of the function to the calling function main().

Arguments and One Return Value

In this type, a function has arguments as well as a return value, so it receives values from the calling function. Similarly, it returns a value, so the calling function receives a value from the called function.

Below is an example to demonstrate the use of function with arguments and one return value.

Example: Program to add two numbers using a user-defined function.

#include<stdio.h>
int Add(int,int);
int main()
{
  int Sum,x,y;
  printf("Enter two mumbers:");
  scanf("%d%d",&x,&y);
  Sum = Add(x,y);
  
  printf("%d",Sum);
  return 0;
}
int Add(int x,int y)
{
  int n;
  n=x+y;
  return n; 
}

Output:

Enter two numbers:6
3
9

Explanation:

In line 8 of the above code, arguments x and y are passed in the function Add() inside the function main().
Similarly, in line 17, function Add() returns the value of n to the calling function main().

In the next section, we will discuss a very useful concept in C called Recursion in detail.