Categories of functions

Categories of functions :

Functions can be be broadly categories into four ways depending upon whether arguments are passed or not and whether a value is returned or not.
  • A function with no parameter and no return value.
  • A function with parameters and no return value.
  • A function with parameter and return value.
  • A function with no parameters but return value.

  • No parameter and no return value :

A function which is invoked without passing any arguments from the calling function is a function with no parameters. If such function does not return any value also then such a function is an example of a function with no parameter and no return value. In such functions, there is no transfer of any data the called function to the calling function and vice versa. Some examples of such function are:
  void displayMenu();
  void programInfo();
  void author_information();

Program in which a function accepts no parameter and no return value ?
#include<stdio.h>
#include<conio.h>
void sumDisplay();       /*function call*/
getch();
}
void sumDisplay()   /* no parameter, no return value */
{
int a, b, res;
printf("enter value of two numbers to add = ");
scanf("%d%d, &a,&b);
res = a+b;
printf("%d +%d = %d", a,b, res);
}

OUTPUT:
enter value of two numbers to add = 3    5
3 + 5= 8

Explanation:In this program , we show the use of functions with no parameter and no return type. The statements sumDisplay(); in main() call the sumDisplay() function and as a result the control shifts to this function. The statements in its body are executed. Here two numbers a and b are input and their sum res is displayed. After completing the execution of all the statements , the control returns back to main().


  • Functions with parameters but no return value:
A function which is invoked by passing some arguments from the calling function but does not return back any value back to the calling function is a function with parameters but no return value. There exist only one way of communication of data from the calling function to the called function.

Program which accepts arguments and returns no value?
#include<stdio.h>
#include<conio.h>
void calSum(int, int);
main()
{
int a,b,sum;
clrscr();
printf("enter value of a and b : ");
scanf("%d%d", &a,&b);
calSum(a,b);
getch();
}
void calSum(int  x, int  y)  /*two parameters but no return value */
{
int res;
res = x+y;
printf("%d + %d = %d", x, y, res);
enter value of a and b : 3     5
3+5=8

Explanation: In this program, when calSum() is called from within main(), value of arguments a and b is passed in the formal parameters x and y in the function definition. The statements in the function body are executed. The sum of these numbers is calculated and is printed in the function itself. However, no value is returned back to the calling function main() i.e. there is one-way data communication between the calling function and called function.

  • function with parameters and return value:
A function with parameters and return type , on invocation value of arguments to called function and return a value back to the calling function. Thus, a two -way communication is made possible between the calling function and called function.

Program to calculate the sum of two number to show a function accepts arguments and returns value?
#include<stdio.h>
#include<conio.h>
int calSum(int, int);     /*Function declaration */
main()
{
int a,b, res;
clrscr();
printf("enter two whose sum to calculate :");
scanf("%d%d",&a,&b);
res =cvalSum(a,b);  /*function call */
printf("%d + %d = %d", a, b,res);
getch();
}
int calSum(int  x, int  y)  /* value passed of type int*/
{
int total;
total  =  x + y;
return(total);    /*data type of value returned same as return type */
}

OUTPUT:
enter two whose sum to calculate :  3    5
3  +  5 = 8

Explanation: In the above program, there is a two way data communication between calling function and the called function. The value of variables a and b are inputted in the main() and are passed to the function calSum(). After executing the statements in its body, the value of total is returned back to main() where it is assigned to res. The value stored in res is then printed.

  • Function with no parameter but return value :
A function with no parameter but return value, on invocation passes no value to the called function but a value is returned back to the calling function. So again there is one way communication made between the calling function and the called function.

Program is which function accepts no arguments but return some value?
#include<stdio.h>
#include<conio.h>
int calSum(void)
main()
{
int res;
clrscr();
res = calSum();   /*function call - no value passed*/
printf("sum= %d",res);
getch();
}
int  calSum(void)
{
int a,b,sum;
printf("enter two numbers that you want to add: ");
scanf("%d%d",&a,&b);
sum=a+b;
return (sum);  /*value returned*/
}

OUTPUT:
enter two numbers that you want to add: 3     5
sum=8

Explanation:In this program , there is also one way data communication but in opposite direction. The data can be transferred only from the calling function. The value of two numbers a and b is inputted on execution of the function call. The sum of a and b is then returned to the calling function main().

                                              THANKS




No comments

Powered by Blogger.