In this tutorial, we will learn about types of user defined functions in c programming.

types of functions in c programming

Types of user defined functions in C

Depending upon the presence of arguments and the return values, user defined functions can be classified into five categories.

  1. Function with no arguments and no return values
  2. Function with no arguments and one return value
  3. Function with arguments and no return values
  4. Function with arguments and one return value
  5. Function with multiple return values

1: Function with no arguments and no return value


Function with no argument means the called function does not receive any data from calling function and Function with no return value means calling function does not receive any data from the called function. So there is no data transfer between calling and called function.

C program to calculate the area of square using the function with no arguments and no return values


/* program to calculate the area of square */
#include <stdio.h>
void area();   //function prototype
int main()
{
  area();    //function call
  return 0;
}
void area()
{
  int square_area,square_side;
  printf("Enter the side of square :");
  scanf("%d",&square_side);
  square_area = square_side * square_side;
  printf("Area of Square = %d",square_area);
}

Explanation

In the above program, area( ); function calculates area and no arguments are passed to this function. The return type of this function is void and hence return nothing.

[adsense1]

2: Function with no arguments and one return value


As said earlier function with no arguments means called function does not receive any data from calling function and function with one return value means one result will be sent back to the caller from the function.

C program to calculate the area of square using the function with no arguments and one return values

#include <stdio.h>
int area();   //function prototype with return type int
int main()
{
  int square_area;
  square_area = area();    //function call
  printf("Area of Square = %d",square_area);
  return 0;
}
int area()
{
  int square_area,square_side;
  printf("Enter the side of square :");
  scanf("%d",&square_side);
  square_area = square_side * square_side;
  return square_area;
}

Explanation
In this function int area( ); no arguments are passed but it returns an integer value square_area.

3: Function with arguments and no return values


Here function will accept data from the calling function as there are arguments, however, since there is no return type nothing will be returned to the calling program. So it’s a one-way type communication.

C program to calculate the area of square using the function with arguments and no return values

#include <stdio.h>
void area( int square_side);  //function prototype
int main()
{
  int square_side;
  printf("Enter the side of square :");
  scanf("%d",&square_side);
  area(square_side);   //function call
  return 0;
}
void area(int square_side)
{
  int square_area;
  square_area = square_side * square_side;
  printf("Area of Square = %d",square_area);
}

Explanation
In this function, the integer value entered by the user in square_side variable is passed to area(); .The called function has void as a return type as a result, it does not return value.

This program calculates the area of a square and prints the result.

4: Function with arguments and one return value


Function with arguments and one return value means both the calling function and called function will receive data from each other. It’s like a dual communication.

C program to calculate the area of square using the function with arguments and one return values

#include <stdio.h>
int area(int square_side);   //function prototype with return type int
int main()
{
  int square_area,square_side;
  printf("Enter the side of square :");
  scanf("%d",&square_side);
  square_area = area(square_side);    //function call
  printf("Area of Square = %d",square_area);
  return 0;
}
int area(int square_side)
{
  int square_area;
  square_area = square_side * square_side;
  return square_area;
}

5: Function with multiple return values


So far we have used functions that return only one value because function normally returns a single value. However, we can use functions which can return multiple values by using input parameters and output parameters. Those parameters which are used to receive data are called input parameters and the parameters used to send data are called output parameters. This is achieved by using address operator(&) and indirection operator(*). Moreover, following example will clarify this concept.

C program to calculate the area and volume of square using the function with multiple return values

#include <stdio.h>
void area_volume(int l, int *a, int *v);   //function prototype 
int main()
{
  int l,a,v;
  printf("Enter the side of square :");
  scanf("%d",&l);
  area_volume(l,&a,&v);    //function call
  printf("Area = %d\n Volume = %d",a,v);
  return 0;
}
void area_volume(int l, int *a, int *v)
{
 *a = l*l;
 *v = l*l*l;
}

Explanation

In the above program l is input argument, a and v are output arguments. In the function call, we pass actual value of l whereas addresses of a and v are passed.