Types of function invoking

We categorize a function’s invoking (calling) depending on arguments or parameters and their returning a value. In simple words, we can divide a function’s invoking into four types depending on whether parameters are passed to a function or not and whether a function returns some value or not.

The various types of invoking functions are:

• With no arguments and with no return value.
• With no arguments and with the return value
• With arguments and with no return value
• With arguments and with the return value.

Let us discuss each category with some examples:

TYPE 1: With no arguments and have no return value

As the name suggests, any function which has no arguments and does not return any values to the calling function, falls in this category. These type of functions are confined to themselves i.e. neither do they receive any data from the calling function nor do they transfer any data to the calling function. So there is no data communication between the calling and the called function are only program control will be transferred.

/* Program for illustration of the function with no arguments and no return value*/
/* Function with no arguments and no return value*/
#include <stdio.h>
main()
{
void message();
printf(“Control is in main\n”);
message(); /* Type 1 Function */
printf(“Control is again in main\n”);
}
void message()
{
printf(“Control is in message function\n”);
} /* does not return anything */

OUTPUT

Control is in main
Control is in message function
Control is again in main

TYPE 2: With no arguments and with the return value

Suppose if a function does not receive any data from calling function but does send some value to the calling function, then it falls in this category.
Write a program to find the sum of the first ten natural numbers.

/* Program to find sum of first ten natural numbers */
#include <stdio.h>
int cal_sum()
{
int i, s=0;
for (i=0; i<=10; i++)
s=s + i;
return(s); /* function returning sum of first ten natural numbers */
}
main()
{
int sum;
sum = cal_sum();
printf(“Sum of first ten natural numbers is % d\n”, sum);
}

OUTPUT

Sum of the first ten natural numbers is 55

TYPE 3: With Arguments and have no return value

If a function includes arguments but does not return anything, it falls in this category. One way communication takes place between the calling and the called function.

Before proceeding further, first, we discuss the type of arguments or parameters here. There are two types of arguments:

• Actual arguments
• Formal arguments

Let us take an example to make this concept clear:

Write a program to calculate the sum of any three given numbers.

#include <stdio.h>
main()
{
int a1, a2, a3;
void sum(int, int, int);
printf(“Enter three numbers: “);
scanf (“%d%d%d”,&a1,&a2,&a3);
sum (a1,a2,a3); /* Type 3 function */
}
/* function to calculate sum of three numbers */
void sum (int f1, int f2, int f3)
{
int s;
s = f1+ f2+ f3;
printf (“\nThe sum of the three numbers is %d\n”,s);
}

OUTPUT

Enter three numbers: 23 34 45
The sum of the three numbers is 102

Here f1, f2, f3 are formal arguments and a1, a2, a3 are actual arguments.
Thus we see in the function declaration, the arguments are formal arguments, but when values are passed to the function during a function call, they are actual arguments.

Note: The actual and formal arguments should match in type, order and number

TYPE 4: With arguments function and with the return value

In this category, two-way communication takes place between the calling and called function i.e. a function returns a value and also arguments are passed to it. We modify the above Example according to this category.
Write a program to calculate the sum of the three numbers.

/*Program to calculate the sum of three numbers*/
#include <stdio.h>
main ( )
{
int a1, a2, a3, result;
int sum(int, int, int);
printf(“Please enter any 3 numbers:\n”);
scanf (“%d %d %d”, & a1, &a2, &a3);
result = sum (a1,a2,a3); /* function call */
printf (“Sum of the given numbers is : %d\n”, result);
}
/* Function to calculate the sum of three numbers */
int sum (int f1, int f2, int f3)
{
return(f1+ f2 + f3); /* function returns a value */
}

OUTPUT

Please enter any 3 numbers:
3 4 5
Sum of the given numbers is: 12

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
?>