Types of Functions in C

In real-time, a function in C may define with or without parameters, and a function may or may not return a value. It entirely depends upon the user requirement. In C Programming, as per our requirement, we can define the User-defined functions in multiple ways. The following are a list of available types of Functions in C

  1. Function with no argument and no Return value
  2. Function with no argument and with a Return value
  3. Function with argument and No Return value
  4. Function with argument and Return value

From the above, 1 and 3 types do not return any value when we call the function. So, We use the void return type while defining the function. 

When we call the functions 2 and 4 types, they will return some value. That’s why we have to use the appropriate data type (int, float, double, etc.) as a return type while defining the function. We use the return keyword inside the function to return some value when we call the function from the main() function or any sub-functions.

Types of Functions in C Programming

The following examples will explain to you the available function types in C programming.

C Function with No argument and No Return value

In this method, We won’t pass any arguments to the function while defining, declaring, or calling the function. This type of functions in C will not return any value when we call the function from main() or any sub-function. When we are not expecting any return value, but we need some statements to print as output. Then, this type of function in C is very useful.

C Function with No argument and No Return value Example

In these types of Functions in C program, We are going to calculate the Sum of 2 integer values and print the output from the user-defined function itself.

/* Function with No argument and No Return value Example */
#include<stdio.h>

// Function Declaration
void Addition();        

void main()
{
  printf("\n ............. \n");
  
  Addition();                      
}

void Addition()
{
  int Sum, a = 10, b = 20;  
  Sum = a + b;
  
  printf("\n Sum of a = %d and b = %d is = %d", a, b, Sum);
}
 ............. 

 Sum of a = 10 and b = 20 is = 30

Within these types of Functions in C example, If you observe the main() function, We haven’t passed any arguments /parameters to the function Addition()

Within the Addition function, we declared the integer variables of the sum, a, b, and we assigned 10 to a and 20 to b. In the next C Programming line, we calculate the sum using Arithmetic operator ( + )

Sum = a + b
    = 10 + 20
    = 30

Below printf statement is used to print the output.

printf("\n Sum of a = %d and b = %d is = %d", a, b, Sum);

Whenever we call the Addition () function, it will print the same output because the values of a and b are fixed inside the function.

C Function with no argument and with Return value

In this method, We won’t pass any arguments to the function while defining, declaring, or calling the function. This type of function will return some value when we call the function from main() or any subfunction.

The Data Type of the return value will depend upon the return type of function declaration. For instance, if the return type is int then return value will be int.

Function with No arguments and with Return value Example

In this program, We are going to calculate the multiplication of 2 integer values using the user-defined function without arguments and return keyword.

#include<stdio.h>

int Multiplication();        

int main()
{
  int Multi;

  Multi = Multiplication();
  printf("\n Multiplication of a and b is = %d \n", Multi );        

  return 0;            
}

int Multiplication()
{
  int Multi, a = 20, b = 40;  
  
  Multi = a * b;

  return Multi;
}
 Multiplication of a and b is = 800 

If you observe the main() function, we declared the integer variable Multi. We assigned it to the return value of the Multiplication () function. Because user-defined function also returns integer value only.

Multi = Multiplication();

We haven’t passed any arguments /parameters to the function Multiplication()

In the next line, the printf statement is to print the output

printf("\n Multiplication of a and b is = %d \n", Multi);

Whenever we call the Multiplication () function, it will print the above statement.

Within the Multiplication () function, we declared the integer variables of Multi, a, b, and we assigned 20 to a and 40 to b.

In the next line, we Multiplied both a and b using Arithmetic operator ( * )

Multi = a * b
      = 20 * 40
      = 800

Whenever we call the Multiplication () function, it prints the same output because the values of a and b are the same inside the function.

C Function with argument and No Return value

If you observe the above two methods, No matter how many times you executive, it will give the same output. We don’t have any control over the values of the variables a and b because they are fixed values.

In real-time, we mostly deal with dynamic data means we have to allow the user to enter his own values rather than fixed ones.

This method allows us to pass the arguments to the function while calling the function. But, This type of function will not return any value when we call the function from main () or any subfunction.

If we want to allow the user to pass his data to the function arguments, but we are not expecting any return value, this type of function is very useful.

Function with argument and No Return value Example

These Types of Functions in C program allows the user to enter 2 integer values. Next, We are going to pass those values to the user-defined function to calculate the sum.

#include<stdio.h>

void Addition(int, int);        

void main()
{
  int a, b;

  printf("\n Please Enter two integer values \n");
  scanf("%d %d",&a, &b);

  //Calling the function with dynamic values
  Addition(a, b);
}

void Addition(int a, int b)
{
  int Sum;  
  
  Sum = a + b;

  printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);
}
 Please Enter two integer values 
40
90

 Additiontion of 40 and 90 is = 130 

If you observe the main() function, We declared the integer variables a and b. This program allows the user to enter 2 integer values as a and b.

printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);

In the next line, we called the Addition (int a, int b) function with the user entered values

Addition(a, b);

Within the Addition (int a, int b) function,

We declared the integer variables of Sum, and also we have integer (a, b) arguments in the function. It means this function will allow the user to pass 2 integer values.

Next, we added both a and b using Arithmetic operator ( + ). In the next line, the printf statement is to print the output

printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);

C Function with argument and Return value

This method allows us to pass the arguments to the function while calling the function. This type of function will return some value when we call the function from main () or any subfunction. Data Type of the return value will depend upon the return type of function declaration. For instance, if the return type is int then return value will be int.

This type of user-defined function is called a fully dynamic function, and it provides maximum control to the end-user.

Function with arguments and Return value Example

This Types of Functions in C program allows the user to enter 2 integer values. And then, We are going to pass those values to the user-defined function to multiply those values and return the value using the return keyword.

#include<stdio.h>

int Multiplication(int, int);        

int main()
{
  int a, b, Multi;

  printf("\n Please Enter two integer values \n");
  scanf("%d %d",&a, &b);

  //Calling the function with dynamic values
  Multi = Multiplication(a, b);

  printf("\n Multiplication of %d and %d is = %d \n", a, b, Multi);        
  return 0;            
}

int Multiplication(int a, int b)
{
  int Multi;  

  Multi = a * b;

  return Multi;
}
Types of Functions in C Programming 5

If you observe the main() function, We declared the integer variables Multi, a, and b. This program allows the user to enter 2 integer values as a and b.

printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);

In the next line, we assigned the return value of the Multiplication(int a, int b) function to the Multi integer. It is because the user-defined function also returns integer value only.

Multi = Multiplication(a, b);

We passed the user inputs as the arguments /parameters to the function Multiplication(int a, int b). In the next line, the printf statement prints the output

printf("\n Multiplication of a and b is = %d \n", Multi);

Within the Multiplication () function, we declared the integer variables of Multi, and also we have two integers (a, b) arguments in the function. It means this function will allow the user to pass two integer values. In the next line, we Multiplied both a and b using Arithmetic operator ( * )

Comments are closed.