SlideShare uma empresa Scribd logo
1 de 41
Functions
Topics ,[object Object],[object Object],[object Object],[object Object],[object Object]
Functions ,[object Object],[object Object],[object Object]
Function Calls One function calls another by using the name of the called function next to ( ) enclosing an argument list. A function call temporarily transfers control from the calling function to the called function.
FunctionName ( argument List  ) The argument list is a way for functions to communicate with each other by passing information. The argument list can contain 0, 1, or more arguments, separated by commas, depending on the function.   Function Call Syntax
Two Parts of Function Definition int  Cube ( int  n ) heading {   body return  n  *  n  *  n ; }
What is in a heading?  int  Cube  ( int  n ) type of value returned name of function parameter list
What is in a prototype?  A prototype looks like a heading but must end with a semicolon;  and its parameter list just needs to contain the type of each parameter. int  Cube( int  );   // prototype
When a function is called, temporary memory is set up ( for its value parameters and any local variables, and also for the function’s name if the return type is not void).  Then the flow of control passes to the first statement in the function’s body.  The called function’s body statements are executed until one of these occurs: return statement (with or without a return value), or, closing brace of function body. Then control goes back to where the function was called.
#include <iostream> int Cube ( int ) ; // prototype using namespace std;   void  main (  ) {  int  yourNumber ;      arguments int  myNumber ; yourNumber = 14 ; myNumber  =  9 ; cout << “My Number = “    << myNumber ; cout << “its cube is “ << Cube ( myNumber ) << endl ; cout << “Your Number = “ << yourNumber ; cout << “its cube is “ << Cube ( yourNumber )   << endl ; }
To Compile Successfully, ,[object Object]
A C++ function can return ,[object Object],[object Object]
Write a void function called  DisplayMessage ( ) which you can call from main ( ) to describe the pollution index value it receives as a parameter. Your city describes a pollution Index  less than 35 as “Pleasant”,  35 through 60 as “Unpleasant”,  and above 60 as “Health Hazard.”
  parameter void DisplayMessage( int index ) { if ( index < 35 ) cout << “Pleasant”; else if ( index <= 60 ) cout << “Unpleasant”; else cout << “Health Hazard”; }
#include <iostream> void DisplayMessage (int);  // prototype using namespace std;   int main ( )   argument {  int pollutionIndex; cout << “Enter air pollution index”; cin >> pollutionIndex; DisplayMessage(pollutionIndex);   // call return 0; } The Rest of the Program
return; ,[object Object],[object Object]
Header files contain declarations of   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Program with Several Functions Square  function Cube  function function prototypes main  function
Value-returning Functions #include <iostream>   int  Square ( int ) ;       // prototypes int  Cube ( int ) ; using namespace std;   int  main (  ) {  cout  <<  “The square of 27 is “   <<  Square (27)   <<  endl;   // function call cout  <<  “The cube of 27 is “ <<  Cube (27)   <<  endl;   // function call     return 0; }
Rest of Program int Square ( int  n ) // header and body { return  n * n; } int Cube ( int  n ) // header and body { return  n * n * n; }
A void function call stands alone #include  <iostream> void  DisplayMessage ( int ) ;    // prototype using namespace std; int  main (  )   argument {  DisplayMessage(15); // function call cout  <<  “Good Bye“  <<  endl;   return 0; }
A void function does NOT return a value   parameter void  DisplayMessage ( int  n ) { cout  <<  “I have liked programming for  “  <<  n  <<  “ years”  << endl ; return ; }
Parameter List ,[object Object]
Classified by Location Always appear in  a  function call  within the calling block. Always appear in  the function  heading ,  or function  prototype . Arguments  Parameters
Some C++ Texts ,[object Object],[object Object]
Value Parameter  Reference Parameter The value (25) of the argument  is passed  to the function when  it is called. The memory address (4000) of the argument is passed  to the function when  it is called . 25 4000 age In this case, the  argument can be a  variable identifier, constant, or expression. In this case, the argument must be a variable identifier.  Argument in Calling Block
By default, parameters   (of simple types like int, char, float, double) are always value parameters, unless you do something to change that.   To get a reference parameter  you need to  place & after the type  in the function heading and prototype.
When to Use Reference Parameters  ,[object Object]
Using a Reference Parameter  ,[object Object],[object Object]
MAIN PROGRAM MEMORY If you pass only a copy of 25 to a function, it is called  “pass-by-value”  and the function will not be able to change the contents of age.  It is still 25 when you return. 25 4000 age
MAIN PROGRAM MEMORY BUT, if you pass 4000, the address of age to a function, it is called  “pass-by-reference”  and the function will be able to change the contents of age.  It could be 23 or 90 when you return. 25 4000 age
Pass-by-reference  is also called . . .  ,[object Object],[object Object]
Example of Pass-by-Reference void swap( int &x, int &y) { int tmp; tmp = x; x = y; y = tmp; }
void main() {… a = 5;b = 3; cout<<“Before swap:”      <<a<<b; swap(a,b); cout<<“After swap:”    <<a<<b; } }
Pass-by-value CALLING BLOCK FUNCTION   CALLED “ incoming” value  of argument
Pass-by-reference CALLING BLOCK FUNCTION   CALLED “ outgoing” changed  value  of argument OR, “ incoming” original value  of argument
Questions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
More Questions ,[object Object],[object Object],[object Object],[object Object],[object Object]
Functions are written to specifications ,[object Object],[object Object]
Write prototype and function definition for ,[object Object],[object Object]
void GetRating( char& );     // prototype     void GetRating (char&  letter) { cout << “Enter employee rating.” << endl; cout << “Use E, G, A, or P : ” ; cin >> letter; while ( (letter  !=  ‘E’)  &&  (letter  !=  ‘G’)  &&    (letter  !=  ‘A’)  &&  (letter  !=  ‘P’) ) { cout << “Rating invalid.  Enter again: ”; cin  >> letter; } }

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Function in c
Function in cFunction in c
Function in c
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Function
FunctionFunction
Function
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Function in c
Function in cFunction in c
Function in c
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
user defined function
user defined functionuser defined function
user defined function
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
 
4. function
4. function4. function
4. function
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 

Semelhante a Functions (20)

Ch06
Ch06Ch06
Ch06
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Function in c
Function in cFunction in c
Function in c
 
C function
C functionC function
C function
 
Function C++
Function C++ Function C++
Function C++
 
Function
Function Function
Function
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Functions
FunctionsFunctions
Functions
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Lecture6
Lecture6Lecture6
Lecture6
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 

Último

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 

Último (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 

Functions

  • 2.
  • 3.
  • 4. Function Calls One function calls another by using the name of the called function next to ( ) enclosing an argument list. A function call temporarily transfers control from the calling function to the called function.
  • 5. FunctionName ( argument List ) The argument list is a way for functions to communicate with each other by passing information. The argument list can contain 0, 1, or more arguments, separated by commas, depending on the function. Function Call Syntax
  • 6. Two Parts of Function Definition int Cube ( int n ) heading { body return n * n * n ; }
  • 7. What is in a heading? int Cube ( int n ) type of value returned name of function parameter list
  • 8. What is in a prototype? A prototype looks like a heading but must end with a semicolon; and its parameter list just needs to contain the type of each parameter. int Cube( int ); // prototype
  • 9. When a function is called, temporary memory is set up ( for its value parameters and any local variables, and also for the function’s name if the return type is not void). Then the flow of control passes to the first statement in the function’s body. The called function’s body statements are executed until one of these occurs: return statement (with or without a return value), or, closing brace of function body. Then control goes back to where the function was called.
  • 10. #include <iostream> int Cube ( int ) ; // prototype using namespace std; void main ( ) { int yourNumber ; arguments int myNumber ; yourNumber = 14 ; myNumber = 9 ; cout << “My Number = “ << myNumber ; cout << “its cube is “ << Cube ( myNumber ) << endl ; cout << “Your Number = “ << yourNumber ; cout << “its cube is “ << Cube ( yourNumber ) << endl ; }
  • 11.
  • 12.
  • 13. Write a void function called DisplayMessage ( ) which you can call from main ( ) to describe the pollution index value it receives as a parameter. Your city describes a pollution Index less than 35 as “Pleasant”, 35 through 60 as “Unpleasant”, and above 60 as “Health Hazard.”
  • 14. parameter void DisplayMessage( int index ) { if ( index < 35 ) cout << “Pleasant”; else if ( index <= 60 ) cout << “Unpleasant”; else cout << “Health Hazard”; }
  • 15. #include <iostream> void DisplayMessage (int); // prototype using namespace std; int main ( ) argument { int pollutionIndex; cout << “Enter air pollution index”; cin >> pollutionIndex; DisplayMessage(pollutionIndex); // call return 0; } The Rest of the Program
  • 16.
  • 17.
  • 18. Program with Several Functions Square function Cube function function prototypes main function
  • 19. Value-returning Functions #include <iostream> int Square ( int ) ; // prototypes int Cube ( int ) ; using namespace std; int main ( ) { cout << “The square of 27 is “ << Square (27) << endl; // function call cout << “The cube of 27 is “ << Cube (27) << endl; // function call return 0; }
  • 20. Rest of Program int Square ( int n ) // header and body { return n * n; } int Cube ( int n ) // header and body { return n * n * n; }
  • 21. A void function call stands alone #include <iostream> void DisplayMessage ( int ) ; // prototype using namespace std; int main ( ) argument { DisplayMessage(15); // function call cout << “Good Bye“ << endl; return 0; }
  • 22. A void function does NOT return a value parameter void DisplayMessage ( int n ) { cout << “I have liked programming for “ << n << “ years” << endl ; return ; }
  • 23.
  • 24. Classified by Location Always appear in a function call within the calling block. Always appear in the function heading , or function prototype . Arguments Parameters
  • 25.
  • 26. Value Parameter Reference Parameter The value (25) of the argument is passed to the function when it is called. The memory address (4000) of the argument is passed to the function when it is called . 25 4000 age In this case, the argument can be a variable identifier, constant, or expression. In this case, the argument must be a variable identifier. Argument in Calling Block
  • 27. By default, parameters (of simple types like int, char, float, double) are always value parameters, unless you do something to change that. To get a reference parameter you need to place & after the type in the function heading and prototype.
  • 28.
  • 29.
  • 30. MAIN PROGRAM MEMORY If you pass only a copy of 25 to a function, it is called “pass-by-value” and the function will not be able to change the contents of age. It is still 25 when you return. 25 4000 age
  • 31. MAIN PROGRAM MEMORY BUT, if you pass 4000, the address of age to a function, it is called “pass-by-reference” and the function will be able to change the contents of age. It could be 23 or 90 when you return. 25 4000 age
  • 32.
  • 33. Example of Pass-by-Reference void swap( int &x, int &y) { int tmp; tmp = x; x = y; y = tmp; }
  • 34. void main() {… a = 5;b = 3; cout<<“Before swap:” <<a<<b; swap(a,b); cout<<“After swap:” <<a<<b; } }
  • 35. Pass-by-value CALLING BLOCK FUNCTION CALLED “ incoming” value of argument
  • 36. Pass-by-reference CALLING BLOCK FUNCTION CALLED “ outgoing” changed value of argument OR, “ incoming” original value of argument
  • 37.
  • 38.
  • 39.
  • 40.
  • 41. void GetRating( char& ); // prototype void GetRating (char& letter) { cout << “Enter employee rating.” << endl; cout << “Use E, G, A, or P : ” ; cin >> letter; while ( (letter != ‘E’) && (letter != ‘G’) && (letter != ‘A’) && (letter != ‘P’) ) { cout << “Rating invalid. Enter again: ”; cin >> letter; } }