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

Register FAQ Community Calendar New Posts Navbar Right Corner
HOME BZU Mail Box Online Games Radio and TV Cricket All Albums
Go Back   BZU PAGES: Find Presentations, Reports, Student's Assignments and Daily Discussion; Bahauddin Zakariya University Multan > Institute of Computing > Bachelor of Science in Information Technology > BsIT 3rd Semester > Object Oriented Programming

Object Oriented Programming By Mam Sidra Malik


Reply
 
Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1  
Old 21-10-2008, 10:31 PM
.BZU.'s Avatar


 
Join Date: Sep 2007
Location: near Govt College of Science Multan Pakistan
Posts: 9,693
Contact Number: Removed
Program / Discipline: BSIT
Class Roll Number: 07-15
.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute.BZU. has a reputation beyond repute
lectures What is Function in C++ Fully explained

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<iostream.h>
  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;
              cout<<"\n\n\nbefore swapping x="<<x<<" and y="<<y;
              swap(x,y);
              cout<<"\nafter swapping x="<<x<<" and y="<<y;
  }

  /*output 
   
  before swapping x=9 and y=7
  a=7 and b=9
  after swapping x=9 and y=7 */
2. Pass by address:
It is done through the use of pointer.
Code:
//pass by adress
  #include<iostream.h>
  #include<conio.h>
  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="<<x<<" and y="<<y;
              swap(&x,&y);
              cout<<"\nafter swapping x="<<x<<" and y="<<y;
              getch();
  }
  /*output
   
  before swapping x=9 and y=7
  a=7 and b=9
  after swapping x=7 and y=9 */
3. Pass by reference
We can pass parameter in C++ by reference. While we pass arguments by nickname, the formal arguments in the called function become aliases (nickname) to the calling function i.e. function is actually working on original data.
Code:
//pass by reference
  #include<iostream.h>
  #include<conio.h>
  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="<<x<<" and y="<<y;
              swap(x,y);
              cout<<"\nafter swapping x="<<x<<" and y="<<y;
              getch();
  }
  /*output
   
  before swapping x=9 and y=7
  a=7 and b=9
  after swapping x=7 and y=9 */
CALLING FUNCTION:
·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<iostream.h>
  #include<conio.h>
  int sum(int a,int b)
  {
              int temp;
              temp=a+b;
              return temp;
  }
  void main()
  {
              int x,y,z;
              clrscr();
              cout<<"\nenter x "<<endl;
              cin>>x;
              cout<<"\nenter y "<<endl;
              cin>>y;
              z=sum(x,y);
              cout<<"\nsum="<<z;
              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<iostream.h>
  #include<conio.h>
  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="<<x<<" and y="<<y;
              swap(x,y);
              cout<<"\nafter swapping x="<<x<<" and y="<<y;
              getch();
  }
  /*output
  enter x
  (let) 4
  enter y
  (let) 5
  sum=9
  */
RETURN IN FUNCTION:
  • RETURN BY VALUE.
  • RETURN BY REFERENCE.

1.Return by value:
Syntax:
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<iostream.h>
  #include<conio.h>
  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 "<<max(x,y);
        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<iostream.h>
  #include<conio.h>
  #include<math.h>
  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="<<sum(x,n);
        getch();
  }
  /*
  output
  enter value for x (12)
  how many terms (2)
  sum=84
  */
2. No pass but return
Code:
/no value with return
   
  #include<iostream.h>
  #include<conio.h>
  #include<math.h>
   
  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<<sum;
              return sum;
   
   
  }
  void main()
  {
              cout<<"sumis"<<sum();
              getch();
  }
3. Pass but no return:
Code:
//value with no returns
   
  #include<iostream.h>
  #include<conio.h>
  #include<math.h>
   
  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"<<sum;
              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<iostream.h>
  #include<conio.h>
  #include<math.h>
   
  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<<sum;
              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<iostream.h>
  #include<conio.h>
  int x=5;    //global variable
  void main()
  {
              int x=10;  //local variable
              clrscr();
              cout<<"local varable is "<<x;
              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<iostream.h>
  #include<conio.h>
  void main()
  {
              int x=5;
              clrscr();
              int &y=x;          //y is alias of x
              cout<<"x="<<x;
              y++;
              cout<<"\nx="<<x;
              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<iostream.h>
  #include<conio.h>
  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="<<mul(a,b);
              cout<<"\ndivision="<<div(a,b);
              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="<<m1+m2+m3;
  }
  /*
  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<<volume(10)<<endl;
              cout<<volume(2.5,8)<<endl;
              cout<<volume(1001,75,15)<<endl;
              getch();
  }


Attached Files
File Type: doc What is FUNCTION in C++ Programming Fully Explained.doc (86.5 KB, 357 views)
__________________
(¯`v´¯)
`*.¸.*`

¸.*´¸.*´¨) ¸.*´¨)
(¸.*´ (¸.
Bzu Forum

Don't cry because it's over, smile because it happened
Reply With Quote
Reply

Tags
c++, explained, fully, function, functions, lectures


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
When you fully trust a person Salman Mushtaq Quotes 0 25-09-2011 01:08 PM
Link List (Treasure Hunt for Kids) Fully Explained Lecture slide by Sir Ahsan Raza. .BZU. Data Structure 0 08-12-2008 05:25 PM
Lecture 3, Internet ,Protocols,IP,Networking,Domains etc fully explained (BSIT06-10) .BZU. E Commerce 0 05-12-2008 09:11 PM
Function BSIT07-01 Math for IT-I 1 16-11-2007 11:42 PM

Best view in Firefox
Almuslimeen.info | BZU Multan | Dedicated server hosting
Note: All trademarks and copyrights held by respective owners. We will take action against any copyright violation if it is proved to us.

All times are GMT +5. The time now is 08:08 PM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.