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 return value
• With arguments and with no return value
• With arguments and with 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.
Example
/* Program for illustration of the function with no arguments and no return value*/
#include <stdio.h>
main(){
void message();
printf("Control is in main\n");
message();
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 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.
Example
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 1++)
return(s);
}
main()
{
int sum;
sum=cal_sum();
printf("sum of first ten natural number is % d\n"sum);
}
OUTPUT
Sum o first ten natural number 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 art example to make this concept clear:
Example
Write a program to calculate sum of any three given numbers.
#include <stdio.h>
main(){
int al, a2, a3;
void sum(int, int, int);
printf("Enter three numbers: ");
scanf ("%d%d%d",&al ,&a2,&a3);
sum (al ,a2,a3);
}
void sum(intf1,intf2,intf3)
{
int s;
s=f1+f2+f3;
printf("\n The sum of three numbers is %d\n",s);
}
OUTPUT
Enter three numbers : 25 34 49
The sum of three numbers is 108
TYPE 4: With arguments function and with 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 above Example according to this category.
Example
Write a program to calculate sum of three numbers.
/*Program to calculate the sum of three numbers.*/
#include <stdio main ( )
{ int a I , a2, a3, result;
int sum(int, int, int);
printf("Please enter any 3 numbers.");
scanf %d %d", & at &a2, &a3);
result = sum (al ,a2,a3);
printf ("Sum of the given numbers is : %d\n,result);
}
int sum (int fl, 12, int f3){
retum(fl +f2 + f3);
}
OUTPUT
please enter any 3 numbers:
3 4 5
Sum of the given numbers is: 12
The various types of invoking functions are:
• With no arguments and with no return value.
• With no arguments and with return value
• With arguments and with no return value
• With arguments and with 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.
Example
/* Program for illustration of the function with no arguments and no return value*/
#include <stdio.h>
main(){
void message();
printf("Control is in main\n");
message();
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 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.
Example
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 1++)
return(s);
}
main()
{
int sum;
sum=cal_sum();
printf("sum of first ten natural number is % d\n"sum);
}
OUTPUT
Sum o first ten natural number 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 art example to make this concept clear:
Example
Write a program to calculate sum of any three given numbers.
#include <stdio.h>
main(){
int al, a2, a3;
void sum(int, int, int);
printf("Enter three numbers: ");
scanf ("%d%d%d",&al ,&a2,&a3);
sum (al ,a2,a3);
}
void sum(intf1,intf2,intf3)
{
int s;
s=f1+f2+f3;
printf("\n The sum of three numbers is %d\n",s);
}
OUTPUT
Enter three numbers : 25 34 49
The sum of three numbers is 108
TYPE 4: With arguments function and with 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 above Example according to this category.
Example
Write a program to calculate sum of three numbers.
/*Program to calculate the sum of three numbers.*/
#include <stdio main ( )
{ int a I , a2, a3, result;
int sum(int, int, int);
printf("Please enter any 3 numbers.");
scanf %d %d", & at &a2, &a3);
result = sum (al ,a2,a3);
printf ("Sum of the given numbers is : %d\n,result);
}
int sum (int fl, 12, int f3){
retum(fl +f2 + f3);
}
OUTPUT
please enter any 3 numbers:
3 4 5
Sum of the given numbers is: 12
0 comments:
Post a Comment