Thursday 12 September 2013

function part 3

The function call transfers the control from the calling funtion to the called function.The function call results in the execution of the function definition.

            There are two types of the function calls,
     
            (i) When the function returns a value .

             The general form is,

             variablename=functionname(argument list);

                  e.g.

                        c=sum(a,b);
(ii) When the function doesnot return a value .
                  The general form is ,
                        functioname(argument list);
                  e.g.

                        show(ch,n);

(c) Function definition  :

            The function definition will define the actual logic of         the function .

            The general form of the function definition is ,

            returntype functionname(argument list)
            {
                  body of the function

            }

Q Write a "C" function to add two numbers.

 #include<iostream.h>
 #include<conio.h>
/*function prototype*/
 int sum(int,int);
void main( )
 {
            int a,b,c;

            clrscr();

      cout<<endl<<"Enter the value of a and b:";
            cin>>a>>b;
     
            c=sum(a,b);/*function call*/

            cout<<endl<<"Sum = "<<c;

            getch();
}
/*function definition */

int sum(int a,int b)
{
      int c;
     
      c=a+b;

      return c;
}
     
(b) When the function doesnot return the value .

     
 #include<iostream.h>
 #include<conio.h>
/*function prototype*/
void sum(int,int);
void main( )
 {
            int a,b;

            clrscr();
cout<<endl<<"Enter the value of a and b:";
cin>>a>>b;
sum(a,b);/*function call*/

           

            getch();
}
/*function definition */

void sum(int a,int b)
{
      int c;
     
      c=a+b;


      cout<<endl<<"Sum = " <<c;

No comments:

Post a Comment