BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan

BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan (http://bzupages.com/)
-   Object Oriented Programming (http://bzupages.com/35-object-oriented-programming/)
-   -   What is Function in C++ Fully explained (http://bzupages.com/f35/what-function-c-fully-explained-1035/)

.BZU. 21-10-2008 10:31 PM

What is Function in C++ Fully explained
 
1 Attachment(s)
What is Function in C++ Fully explained


Code:

  void show ();                //function declaration   
  main ()
  {
                                     
          show ();              //function call
  }
  void show ()
  {
          ---------              //function body
          ---------
          ---------
  }
 
  main ()
  {
          __________
          __________
  }

In C, above statement is valid because the main () in C dose not return any value i.e. the function is void type.
In C++, the main () function return a value of type int to the operating system. Therefore explicitly define main () function as the matching of
int main ()]

Function prototype
·Gives the compiler the details about the functions such as the number and the type of return values.
·It is declaration statement
type function_name (argument_list):
E.g. int area (int x, int y);
float volume (int x, int y, int z);
·Function can also declared as empty argument list as in the following example:
Void display ();
It means that the function doesn't pass any parameters. It is similar to
Void display (void);

PASSING VARIABLES IN A FUNCTION:
1.PASSING BY VALUE.
2.PASSING BY REFERENCE.
3.PASSING BY ADRESS.

1. Passing by value
In it, value is passed directly into a function and function creates new variables to hold the values of these variable argument then the actual arguments remained unaltered.
Code:

//pass by value
  #include
  void swap(int a,int b)
  {
              int temp=a;
              a=b;
              b=temp;
              cout<<"\na="<
Code:

//pass by adress
  #include
  #include
  void swap(int *a,int *b)
  {
              int temp=*a;
              *a=*b;
              *b=temp;
              cout<<"\na="<<*a<<" and b="<<*b;
  }
  void main()
  {
              int x=9,y=7;
              clrscr();
              cout<<"\n\n\nbefore swapping x="<
Code:

//pass by reference
  #include
  #include
  void swap(int &a,int &b)
  {
              int temp=a;
              a=b;
              b=temp;
              cout<<"\na="<·CALL BY VALUE.
·CALL BY REFERENCE.

1. Call by value:
·Function call passes arguments by value
·Called function creates new set of variables and copies the values of argument into them.
·Function dose not have access to the actual variables in the calling program and can only work on the copies of values.
Code:

//call by value
  #include
  #include
  int sum(int a,int b)
  {
              int temp;
              temp=a+b;
              return temp;
  }
  void main()
  {
              int x,y,z;
              clrscr();
              cout<<"\nenter x "<               cin>>x;
              cout<<"\nenter y "<               cin>>y;
              z=sum(x,y);
              cout<<"\nsum="<               getch();
  }
  /*output

  enter x
  (let) 4
  enter y
  (let) 5
  sum=9
  */

2. Call by reference:
It is used to alter the values of the original variables in the calling program. Function actually works on aliases.
Code:

//call by reference
  #include
  #include
  void swap(int &a,int &b)
  {
              int temp=a;
              a=b;
              b=temp;
              cout<<"\na="<
Code:

int sum(int x, int y)
  {
              int z=x+y;
  return z;
  }

2. Return by reference:
Syntax:
int & max(int &x, int &y)
Code:

{
              if (x>y)
                          return x;
              else
                          return y;
  }

  • Return type of max () is int&.
  • The function return reference to x or y (and not the value)
  • The function call such as max (a, b) will yield a reference to either a or b depending on their values.
  • max (a, b) =-1; is legal and assigns -1 to a if it is large otherwise -1 to b.

Q. Write a program that illustrate pass by reference and return by reference.
Code:

  #include
  #include
  int &max(int &a,int &b)
  {
        if(a>b)
                    return a;
        else
                    return b;
  }
  void main()
  {
        int x,y;
        clrscr();
        cout<<"enter the first value ";
        cin>>x;
        cout<<"\nenter the second value ";
        cin>>y;
        cout<<"\nThe greater is "<         getch();
  }
  /*output
  enter the first value (let)5
  enter the second value (let)4
  The greater is 5
  */

TYPES OF FUNCTION:
  • PASS AND RETURN
  • NO PASS BUT RETURN
  • PASS BUT NO RETURN
  • NO PASS AND NO RETURN

1.Pass and return
Code:

//value and return
  #include
  #include
  #include
  float sum(int x, int n)
  {
        float sum=0;
        for(int i=1;i<=n;i++)
        sum+=(float)pow(x,i)/i;
        return sum;
  }
  void main()
  {
        int x,n;
        clrscr();
        cout<<"enter value for x ";
        cin>>x;
        cout<<"how many terms ";
        cin>>n;
        cout<<"sum="<         getch();
  }
  /*
  output
  enter value for x (12)
  how many terms (2)
  sum=84
  */

2. No pass but return
Code:

/no value with return
 
  #include
  #include
  #include
 
  float sum()
  {
              int x,n;
              float sum=0;
              clrscr();
              cout<<"enter value for x";
              cin>>x;
              cout<<"how many terms";
              cin>>n;
              for(int i=1;i<=n;i++)
              sum+=(float)pow(x,i)/i;
              cout<               return sum;
 
 
  }
  void main()
  {
              cout<<"sumis"<               getch();
  }

3. Pass but no return:
Code:

//value with no returns
 
  #include
  #include
  #include
 
  void sum(int x, int n)
  {
 
              float sum=0;
              clrscr();
 
              for(int i=1;i<=n;i++)
              sum+=(float)pow(x,i)/i;
              cout<<"sum is"<               getch();
 
 
  }
  void main()
  {
              int x,n;
              clrscr();
              cout<<"enter value for x";
              cin>>x;
              cout<<"how many terms";
              cin>>n;
              sum(x,n);
              getch();
 
  }

4. No pass and no return
Code:

//no value no return
  #include
  #include
  #include
 
  void sum()
  {
              int x,n;
              float sum=0;
              clrscr();
              cout<<"enter value for x";
              cin>>x;
              cout<<"how many terms";
              cin>>n;
              for(int i=1;i<=n;i++)
              sum+=(float)pow(x,i)/i;
              cout<               getch();
 
 
  }
  void main()
  {
              sum();
  }


SCOPE RESOLUTION OPERATOR ( :: )
C++ supports a mechanism to excess a global variable from a function in which a local variable is defined with the same name as a global variable. It is achieved using scope resolution operator.
Syntax: :: Global variable
It directs the compiler to excess the global variable instead of local variable with the same variable name.
Code:

//use of scope resolution operator
  #include
  #include
  int x=5;    //global variable
  void main()
  {
              int x=10;  //local variable
              clrscr();
              cout<<"local varable is "<               cout<<"\nglobal variable is "<<::x;
              getch();
  }
  /*
  output
  local variable is 10
  global variable is 5
  */

REFERENCE VARIABLE:
A reference variable provides alias that is alternative name of the variable i.e. previously defined.
Code:

// example of reference variable
  #include
  #include
  void main()
  {
              int x=5;
              clrscr();
              int &y=x;          //y is alias of x
              cout<<"x="<               y++;
              cout<<"\nx="<               getch();
  }
  /*
  output
  x=5
  x=6
  */

INLINE FUNCTION:
While inline function is called the inline code of the function is inserted at the place of the function call and compiled with other source code together. By using inline function execution time is reduced because there is no transfer and return back to control. But if function have long code inline function isn't suitable.
·It saves memory.
·The function needn't be duplicated in memory
·It is analogous to macros in C. the major drawback with macros is that they are not really function and therefore the usual error checking doesn't occur during compilation.

Some of situations where inline expansion may not work are:
1.For function returning values if a loop, a switch or goto exists.
2.For functions not returning values, if a return statement exists.
3.If function contain static variables.
4.If inline functions are recursive.
Code:

//example of inline
  #include
  #include
  inline float mul(float x, float y)
  {
              return(x*y);
  }
  inline double div(double p,double q)
  {
              return (p/q);
  }
  void main()
  {
              float a=12.5;
              float b=9.82;
              clrscr();
              cout<<"multiplication="<               cout<<"\ndivision="<               getch();
  }
  /*
  output
  multiplication=122.75
  division=1.272912
  */

DEFAULT ARGUMENTS:
·C++ allows us to call a function without specifying all its arguments. In such case the function assigns a default value to the program which doesn't have a matching argument in the function call.
·Default values are specified when the function is declared.
·We must add the default from right to left.
HTML Code:

//example of default argument
  #include<iostream.h>
  #include<conio.h>
  void tot(int m1=40,int m2=40,int m3=40);
  void main()
  {
              clrscr();
              tot();                //display 120
              tot(45);        //display 125
              tot(45,55);    //display 140
              tot(75,85,96);  //display 226
              getch();
  }
  void tot(int m1,int m2,int m3)
  {
              cout<<"\ntotal marks="<   }
  /*
  output
              display 120
              display 125
              display 140
              display 226
  */

FUNCTION OVERLOADING:
Function overloading refers to the use of same function name for different task. While an overloaded function is called the function with matching argument and return type is invoked.
int max(int, int);
int max(int, int, int);
float max(float, float);
A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.
The function selection involves the following steps:
1.The complier first tries to find an exact match in which the type of actual arguments are the same and used that function.
2.If an exact match is not found, the complier uses integral promotion to the actual arguments such as-char to int float to double to find match.
3.When either of them fails, the complier tries to use built in conversion (the impulsive assignment conversion) to the actual arguments and then uses the function where match is unique. If the conversion is possible to have multiple messages then the compiler will generate the error message. E.g.
long square(long n);
double square(double x);
A function call such as square(10) will cause an error because int arguments can be converted to either long are double.
4. If all the slopes fail then the complier will try the s=user defined conversion in combination with integral promotion and built in conversion to find the unique match(). User defined conversion are often used in handling class and object.
HTML Code:

//example of function overloading
  #include<iostream.h>
  #include<conio.h>
  int volume(int s)
  {
              return(s*s*s);
  }
  double volume(double r, int h)
  {
              return(3.14159*r*r*h);
  }
  long volume(long l,int b, int h)
  {
              return(l*b*h);
  }
  void main()
  {
              clrscr();
              cout<               cout<               cout<               getch();
  }




All times are GMT +5. The time now is 04:45 AM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.