Types of Functions in C Programming

             A function may belong to any one of the following categories:    
1. Functions with no arguments and no return values.    
2. Functions with arguments and no return values.    
3. Functions with arguments and return values.       



Functions with no arguments and no return values     


Let us consider the following program    
/* Program to illustrate a function with no argument and no return values*/   
 #include    
main()   
 {            
statement1();           
 starline();             
statement2();           
 starline();    
}   
 /*function to print a message*/    
statement1()   
 {           
 printf(“\n Sample subprogram output”);   
 }    
statement2()    
{            
printf(“\n Sample subprogram output two”);    
}     
starline()   
 {         
 int a;          
for (a=1;a<60 a="" nbsp="" o:p="">
 printf(“%c”,‟*‟);          
  printf(“\n”);     }  
  
In the above example there is no data transfer between the calling function and the called function.  When a function has no arguments it does not receive any data from the calling function. Similarly  when it does not return value the calling function does not receive any data from the called function. A  function that does not return any value cannot be used in an expression it can be used only as independent statement.    


Functions with arguments but no return values    

The nature of data communication between the calling function and the arguments to the called function and the called function does not return any values to the calling function this shown in example below:
      
Consider the following:
    
Function calls containing appropriate arguments. For example, the function call value (500,0.12,5)  Would send the values 500,0.12 and 5 to the function value (p, r, n) and assign values 500 to p, 0.12 to r  and 5 to n. the values 500,0.12 and 5 are the actual arguments which become the values of the formal  arguments inside the called function.     Both the arguments actual and formal should match in number type and order. The values of actual  arguments are assigned to formal arguments on a one to one basis starting with the first argument as  shown below:
main()          
{                     
 function1(a1,a2,a3……an)          
}             
 function1(f1,f2,f3….fn);          
{                  
function body;          
}     
 Here a1, a2, a3 are actual arguments and f1, f2, f3 are formal arguments.
    
The no of formal arguments and actual arguments must be matching to each other suppose if actual  arguments are more than the formal arguments; the extra actual arguments are discarded. If the numbers of actual arguments are less than the formal arguments, then the unmatched formal arguments are initialized to some garbage values. In both cases no error message will be generated.     The formal arguments may be valid variable names; the actual arguments may be variable names  expressions or constants. The values used in actual arguments must be assigned values before the  function call is made.   When a function call is made only a copy of the values actual arguments is passed to the called  function. What occurs inside the functions will have no effect on the variables used in the actual?  argument list.      

Let us consider the following program  
   
  /*Program to find the largest of two numbers using function*/      
#include<stdio.h>   
main()   
 {          
int a,b;             
printf(“Enter the two numbers”);              
scanf(“%d%d”,&a,&b);            
largest(a,b);   
 }    
/*Function to find the largest of two numbers*/    
largest(int a, int b)   
 {            
if(a>b)                  
printf(“Largest element=%d”,a);          
 else                  
printf(“Largest element=%d”,b);    
}    


in the above program we could make the calling function to read the data from the terminal and pass it  on to the called function. But function does not return any value.      


Functions with arguments and return values    

  The function of the type Arguments with return values will send arguments from the calling function to the called function and expects the result to be returned back from the called function back to the   calling function.   To assure a high degree of portability between programs a function should generally be coded without  involving any input output operations. For example, different programs may require different output  formats for displaying the results. These shortcomings can be overcome by handing over the result of a  function to its calling function where the returned value can be used as required by the program.
Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours