Function arguments and calling
Function arguments and calling
A function in C can be called either with arguments or without arguments. These function may or may not return values to the calling functions. Also, they may or may not return any values.
Function with no argument and no return value : When a function has no arguments, it does not receive any data from the calling function. Similarly when it does not return a value, the calling function does not receive any data from the called function.
Syntax :
Function declaration : void function();
Function call : function();
Function definition :
void function()
{
statements;
}
Function with arguments but no return value : When a function has arguments, it receive data from the calling function but it returns no values.
Syntax :
Function declaration : void function ( int );
Function call : function( x );
Function definition:
void function( int x )
{
statements;
}
Function with no arguments but returns a value : There could be occasions where we may need to design functions that may not take any arguments but returns a value to the calling function. A example for this is getchar function it has no parameters but it returns an integer an integer type data that represents a character.
Syntax :
Function declaration : int function();
Function call : function();
Function definition :
int function()
{
statements;
return x;
}
Function with arguments and return value
Syntax :
Function declaration : int function ( int );
Function call : function( x );
Function definition:
int function( int x )
{
statements;
return x;
}
Example of function with argument:
/* multipliction of two number:*/
#include
#include
int multiply(int a, int b); /* function declaration */
int main()
{
int a,b,result;
printf("enter two number");
scanf("%d %d",&a,&b);
result=multiply(a,b); //function call
printf("result is =%d",result);
getch();
return 0;
}
int multiply(int a, int b)
{
return(a*b); //function
}
Example of function without argument:
/*A simple program to calculate simple interest. */
#include
#include
void simple();
void main()
{
simple();
getch();
}
void simple()
{
int p,t,r,si;
printf("enter value of p,t,r");
scanf("%d %d %d",&p,&t&r);
si=(p*t*r)/100;
printf("%d",si);
}