C Functions

Functions with arguments but no return values:

/*Program to find the largest of two numbers using function*/ 
#include 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); 
} 

/*Return value data type of function*/
main() 
{ 
   float x,y,add(); 
   double sub(0; 
   x=12.345; 
   y=9.82; 
   printf(“%f\n” add(x,y)); 
   printf(“%lf\n”sub(x,y); 
} 
float add(a,b) 
float a,b; 
{ 
return(a+b); 
} 
double sub(p,q) 
double p,q; 
{ 
return(p-q); 
} 
        
We can notice that the functions too are declared along with the variables.
These declarations clarify to the compiler that the return type of the function add is float and sub is double.

Void functions

main() 
{ 
      void starline(); 
	  void message(); 
} 
void printline 
{ 
      statements; 
} 
void value 
{ 
   statements; 
} 
        
The functions that do not return any values can be explicitly defined as void. This prevents any accidental use of these functions in expressions.

Make Comments..!!


Oops!! No posts from user.

Download Android App