In this tutorial, we are going to learn about categories of functions.

C – Categories of Functions:

All the C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Depending on the arguments and return values functions are classified into 4 categories:

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

When a function has no arguments, it does not receive any data from the calling function. similarly, when a function does not return a value, the calling function does not receive any data from the called function.

1. Functions without arguments and without return value:

In effect, there is no data transfer between the calling function and the called function in the category function without arguments and without a return value.

Let’s consider an example of a function without arguments and without a return value:

#include <stdio.h>
void country_capital(void);
void main() {
  country_capital();
}
void country_capital() {
  printf("New Delhi is the capital of India\n");
}

Output:

New Delhi is the capital of India

In the above sample code the function

void country_capital(void);

specifies that the function does not receive any arguments and does not return any value to the main() function.

2. Functions with arguments and without a return value:

When a function definition has arguments, it receives data from the calling function.

The actual arguments in the function call must correspond to the formal parameters in the function definition, i.e. the number of actual arguments must be the same as the number of formal parameters, and each actual argument must be of the same data type as its corresponding formal parameter.

The formal parameters must be valid variable names in the function definition and the actual arguments may be variable names, expressions, or constants in the function call.

The variables used in actual arguments must be assigned values before the function call is made. When a function call is made, copies of the values of actual arguments are passed to the called function.

What occurs inside the function will have no effect on the variables used in the actual argument list. There may be several different calls to the same function from various places with a program.

Let’s consider an example of a function with arguments and without return value:

#include <stdio.h>
void largest(int, int);
void main() {
  int p, q;
  printf("Enter two numbers : ");
  scanf("%d%d" , &p, &q);
  largest(p, q);
}
void largest(int x, int y) {
  if (x > y) {
    printf("Largest element = %d\n", x);
  } else {
    printf("Largest element = %d\n", y);
  }
}

Output:

Enter two numbers : 1
10
Largest element = 10

In the above sample code the function

void largest(int, int);

specifies that the function receives two integer arguments from the calling function and does not return any value to the called function.

When the function call largest(p, q) is made in the main() function, the values of actual arguments p and q are copied into the formal parameters x and y.

After completion of execution of largest(int x, int y) function, it does not return any value to the main() function. Simply the control is transferred to the main() function.

3. Functions without arguments and with a return value:

When a function has no arguments, it does not receive any data from the calling function. When a function returns a value, the calling function receives data from the called function.

Let’s consider an example of a function without arguments and with a return value:

#include <stdio.h>
int sum(void);          
void main() {
  printf("\nSum of two given values = %d\n", sum());
}
int sum() {
  int a, b, total;   
  printf("Enter two numbers : ");
  scanf("%d%d", &a, &b);
  total = a + b;
  return total;       
}

Output:

Enter two numbers : 1
2
Sum of two given values = 3

 

In the above sample code the function

int sum(void);

specifies that the function does not receive any arguments but returns a value to the calling function.

4. Functions with arguments and with a return value:

When a function definition has arguments, it receives data from the calling function.

After taking some desired action, only one value will be returned from called function to the calling a function through the return statement.

If a function returns a value, the function call may appear in any expression, and the returned value used as an operand in the evaluation of the expression.

Let us consider an example of a function with arguments and with return value:

#include <stdio.h>
int largest(int, int, int);
void main() {
  int a, b, c;
  printf("Enter three numbers : ");
  scanf("%d%d%d" , &a, &b, &c);
  printf(" Largest of the given three numbers = %d\n", largest(a, b, c));
}
int largest(int x, int y, int z) {
  if ((x > y) && (x > z)) {
    return x; 
  } else if (y > z) {
    return y;
  } else {
    return z;
  }
}

Output:

Enter three numbers : 1
4
7
Largest of the given three numbers = 7

In the above sample code the function

int largest(int, int, int);

specifies that the function receives three values and returns a value to the calling function.

References:

Happy Learning 🙂

Hemanth Krishna Vineel Ramayanam
I’m an Undergraduate Student at the Indian Institute of Information Technology, Design, and Manufacturing, Kurnool, Andhra Pradesh. I belong to the CSE domain.

Leave A Comment