SlideShare a Scribd company logo
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; } }

More Related Content

What's hot

Function in c
Function in cFunction 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
imtiazalijoono
 
Function
FunctionFunction
Function
jayesh30sikchi
 
functions in C and types
functions in C and typesfunctions in C and types
functions in C and types
mubashir farooq
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
Swapnil Yadav
 
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)
VC Infotech
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
Function lecture
Function lectureFunction lecture
Function lecture
DIT University, Dehradun
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
user defined function
user defined functionuser defined function
user defined function
King Kavin Patel
 
Function in C Language
Function in C Language Function in C Language
Function in C Language
programmings guru
 
4. function
4. function4. function
4. function
Shankar Gangaju
 
Functions in C
Functions in CFunctions in C
Functions in C
Shobhit Upadhyay
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
Praveen M Jigajinni
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 

What's hot (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
 

Similar to Functions

Ch06
Ch06Ch06
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
C function
C functionC function
C function
thirumalaikumar3
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
Function
Function Function
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
Functions
FunctionsFunctions
Recursion in C
Recursion in CRecursion in C
Recursion in C
v_jk
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
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
ankush_kumar
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Lecture6
Lecture6Lecture6
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 

Similar to 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 CRecursion in C
Recursion in C
 
RECURSION IN C
RECURSION IN C RECURSION 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
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 

Recently uploaded

What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Alpen-Adria-Universität
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 

Recently uploaded (20)

What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing InstancesEnergy Efficient Video Encoding for Cloud and Edge Computing Instances
Energy Efficient Video Encoding for Cloud and Edge Computing Instances
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 

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; } }