Function with no argument and no return value, Function with arguments but no return value, Function with arguments and a return value, Function with no argument but returns a value
Categories of functions: A function depends on whether argument is present or not and whether a value is returned or not.
• Function with no argument and no return value.
void add ( void ); // function prototype
void main( )
{
add ( ); // function call.
}
void add (void) // function definition
{ int a=10 , b=20;
cout<<”addition is=”<<a+b;
}
• Function with arguments but no return value. E.g void add ( int a , int b )
void add ( int , int ); // function prototype
void main( )
{ int a,b;
cout<<”enter value of a and b”;
cin>>a>>b;
add (a,b); // function call, a and b are actual arguments.
}
void add (int c, int d) // function definition , c and d are formal arguments.
{
cout<<”addition is=”<<c+d;
}
• Function with arguments and a return value. E.g int add ( int a , int b ).
int add ( int , int ); // function prototype
void main( )
{ int a,b, sum;
cout<<”enter value of a and b”;
cin>>a>>b;
sum= add (a,b); // function call, a and b are actual arguments.
}
int add (int c, int d) // function definition , c and d are formal arguments.
{ int e;
e = c + d;
cout<<”addition is=”<<e;
return( e) ; //u can also pass expression like return( c+d)
}
• Function with no argument but returns a value. E.g int add ( )
int number( void )
{ int m = number( );
cout<<”value is:” <<m;
getch();
}
int number(void)
{ int i;
cin>>i;
return(i);
}
• Function that returns multiple values. //done by call by address.
Labels: C++