SlideShare a Scribd company logo
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Chapter 4:
Functions
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
4.1
Modular Programming
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Modular Programming
• Modular programming: breaking a program up
into smaller, manageable functions or modules
• Function: a collection of statements to perform a
task
• Motivation for modular programming:
– Improves maintainability of programs
– Simplifies the process of writing programs
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
4.2
Defining and Calling Functions
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Defining and Calling Functions
• Function call: statement causes a function
to execute
• Function definition: statements that make
up a function
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Function Definition
• Definition includes:
– return type: data type of the value that function
returns to the part of the program that called it
– name: name of the function. Function names follow
same rules as variables
– parameter list: variables containing values passed to
the function
– body: statements that perform the function’s task,
enclosed in {}
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Function Definition
Note: The line that reads int main()is the
function header.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Function Return Type
• If a function returns a value, the type of the
value must be indicated:
int main()
• If a function does not return a value, its return
type is void:
void printHeading()
{
cout << "Monthly Salesn";
}
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Calling a Function
• To call a function, use the function name
followed by () and ;
printHeading();
• When called, program executes the body of the
called function
• After the function terminates, execution resumes
in the calling function at point of call.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Functions in Program 4-1
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Flow of Control in Program 4-1
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Calling Functions
• main can call any number of functions
• Functions can call other functions
• Compiler must know the following about a
function before it is called:
– name
– return type
– number of parameters
– data type of each parameter
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
4.3
Function Prototypes
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Function Prototypes
• Ways to notify the compiler about a function
before a call to the function:
– Place function definition before calling function’s
definition
– Use a function prototype (function declaration) – like
the function definition without the body
• Header: void printHeading()
• Prototype: void printHeading();
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Program 4-5 (Continued)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Prototype Notes
• Place prototypes near top of program
• Program must include either prototype or full
function definition before any call to the
function – compiler error otherwise
• When using prototypes, can place function
definitions in any order in source file
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
4.4
Sending Data into a Function
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Sending Data into a Function
• Can pass values into a function at time of call:
c = pow(a, b);
• Values passed to function are arguments
• Variables in a function that hold the values
passed as arguments are parameters
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
A Function with a Parameter
Variable
void displayValue(int num)
{
cout << "The value is " << num << endl;
}
The integer variable num is a parameter.
It accepts any integer value passed to the
function.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
The function call in line 11 passes the value 5
as an argument to the function.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Other Parameter Terminology
• A parameter can also be called a formal
parameter or a formal argument
• An argument can also be called an actual
parameter or an actual argument
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Parameters, Prototypes, and
Function Headers
• For each function argument,
– the prototype must include the data type of
each parameter inside its parentheses
– the header must include a declaration for
each parameter in its ()
void evenOrOdd(int); //prototype
void evenOrOdd(int num) //header
evenOrOdd(val); //call
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Function Call Notes
• Value of argument is copied into parameter when
the function is called
• A parameter’s scope is the function which uses it
• Function can have multiple parameters
• There must be a data type listed in the prototype
() and an argument declaration in the function
header () for each parameter
• Arguments will be promoted/demoted as
necessary to match parameters
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Passing Multiple Arguments
When calling a function and passing
multiple arguments:
– the number of arguments in the call must
match the prototype and definition
– the first argument will be used to initialize the
first parameter, the second argument to
initialize the second parameter, etc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Program 6-8 (Continued)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
The function call in line 18 passes value1,
value2, and value3 as a arguments to the
function.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
The return Statement
• Used to end execution of a function
• Can be placed anywhere in a function
– Statements that follow the return statement
will not be executed
• Can be used to prevent abnormal
termination of program
• In a void function without a return
statement, the function ends at its last }
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Program 6-11(Continued)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
4.5
Returning a Value From a
Function
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Returning a Value From a
Function
• A function can return a value back to the
statement that called the function.
• You've already seen the pow function,
which returns a value:
double x;
x = pow(2.0, 10.0);
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Returning a Value From a
Function
• In a value-returning function, the return
statement can be used to return a value from
function to the point of call. Example:
int sum(int num1, int num2)
{
double result;
result = num1 + num2;
return result;
}
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
A Value-Returning Function
int sum(int num1, int num2)
{
double result;
result = num1 + num2;
return result;
}
Return Type
Value Being Returned
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
A Value-Returning Function
int sum(int num1, int num2)
{
return num1 + num2;
}
Functions can return the values of
expressions, such as num1 + num2
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
(Program Continues)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Program 6-12 (Continued)
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
The statement in line 17 calls the sum function,
passing value1 and value2 as arguments.
The return value is assigned to the total variable.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Another Example, from
Program 6-13
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Returning a Value From a
Function
• The prototype and the definition must
indicate the data type of return value
(not void)
• Calling function should use return value:
– assign it to a variable
– send it to cout
– use it in an expression

More Related Content

PPT
chap5 functions.ppt
PPT
PDF
Introduction to C++
PPTX
Functions in python programming and its calling statement
PPTX
User defined functions.1
PPT
270_2_C Functions_Pepper_in C_programming.ppt
PDF
3-Python Functions.pdf in simple.........
PDF
Loops and Files
chap5 functions.ppt
Introduction to C++
Functions in python programming and its calling statement
User defined functions.1
270_2_C Functions_Pepper_in C_programming.ppt
3-Python Functions.pdf in simple.........
Loops and Files

Similar to C++ CH4 in funtion using c++ in all ab.ppt (20)

PDF
Week05
PPTX
CHAPTER THREE FUNCTION.pptx
PPTX
OOP-Module-1-Section-4-LectureNo1-5.pptx
PPT
Functions
PDF
VIT351 Software Development VI Unit1
PPTX
Functions in c
PDF
This is a chapter about arrays and vectors in c++
PPTX
Function
PDF
Handout # 3 functions c++
PPTX
functions in python language to run programming
PPT
Basic information of function in cpu
PPTX
FUNCTIONengineeringtechnologyslidesh.pptx
PPTX
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
PDF
Introduction to Computers and Programming
PPT
programing fundamentals by dr. wheedh khan
PPT
Introduction to computers and programing.pptx
PPT
Procedural Abstraction and Functions That Return a Value.ppt
PDF
Dive into Python Functions Fundamental Concepts.pdf
PPTX
PPTX
UNIT- 2 Functions__________________.pptx
Week05
CHAPTER THREE FUNCTION.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
Functions
VIT351 Software Development VI Unit1
Functions in c
This is a chapter about arrays and vectors in c++
Function
Handout # 3 functions c++
functions in python language to run programming
Basic information of function in cpu
FUNCTIONengineeringtechnologyslidesh.pptx
FUNCTION.pptxfkrdutytrtttrrtttttttttttttt
Introduction to Computers and Programming
programing fundamentals by dr. wheedh khan
Introduction to computers and programing.pptx
Procedural Abstraction and Functions That Return a Value.ppt
Dive into Python Functions Fundamental Concepts.pdf
UNIT- 2 Functions__________________.pptx
Ad

More from MutacalimMohamed (20)

PPTX
HEALTH SERVICE DELIVERY education w.pptx
PPTX
Presentation1 in real word education .pptx
PPTX
New Microsoft PowerPoint Presentation (2).pptx
PPTX
history taking in real world countires .pptx
PPTX
manternal ppt in health science by .pptx
PPTX
Places & Directions English daily uses.pptx
DOCX
Medical Nursing Parasitic Diseases in real.docx
PPTX
New Microsoft PowerPoint Presentation (2).pptx
PPTX
Activities that holds animals in sw.pptx
PPTX
Activities that holds animals in china .pptx
PPT
C++ CH3-P2 using c++ in all other parts.ppt
PPTX
ETHICS by using computing hello in .pptx
PPTX
Presentation2 in human that cases by.pptx
PPTX
Nursing Proposal Defence Presentation.pptx
PPT
ANTIBIOTIC RESISTANCE in whole peolple.ppt
PPT
9- Single Area OSPF on in networking.ppt
PPT
5A- ethernet protocol in engineering.ppt
PPTX
complteyl proposal in GU university.ppt.pptx
PPTX
COMMUNICABLE DISEASE in gu university.pptx
PPTX
project proposal writing in easy way.pptx
HEALTH SERVICE DELIVERY education w.pptx
Presentation1 in real word education .pptx
New Microsoft PowerPoint Presentation (2).pptx
history taking in real world countires .pptx
manternal ppt in health science by .pptx
Places & Directions English daily uses.pptx
Medical Nursing Parasitic Diseases in real.docx
New Microsoft PowerPoint Presentation (2).pptx
Activities that holds animals in sw.pptx
Activities that holds animals in china .pptx
C++ CH3-P2 using c++ in all other parts.ppt
ETHICS by using computing hello in .pptx
Presentation2 in human that cases by.pptx
Nursing Proposal Defence Presentation.pptx
ANTIBIOTIC RESISTANCE in whole peolple.ppt
9- Single Area OSPF on in networking.ppt
5A- ethernet protocol in engineering.ppt
complteyl proposal in GU university.ppt.pptx
COMMUNICABLE DISEASE in gu university.pptx
project proposal writing in easy way.pptx
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Business Ethics Teaching Materials for college
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Basic Mud Logging Guide for educational purpose
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Institutional Correction lecture only . . .
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
2.FourierTransform-ShortQuestionswithAnswers.pdf
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Microbial diseases, their pathogenesis and prophylaxis
Business Ethics Teaching Materials for college
Supply Chain Operations Speaking Notes -ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
RMMM.pdf make it easy to upload and study
Basic Mud Logging Guide for educational purpose
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Institutional Correction lecture only . . .
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES

C++ CH4 in funtion using c++ in all ab.ppt

  • 1. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Chapter 4: Functions
  • 2. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. 4.1 Modular Programming
  • 3. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Modular Programming • Modular programming: breaking a program up into smaller, manageable functions or modules • Function: a collection of statements to perform a task • Motivation for modular programming: – Improves maintainability of programs – Simplifies the process of writing programs
  • 4. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc.
  • 5. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. 4.2 Defining and Calling Functions
  • 6. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Defining and Calling Functions • Function call: statement causes a function to execute • Function definition: statements that make up a function
  • 7. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Function Definition • Definition includes: – return type: data type of the value that function returns to the part of the program that called it – name: name of the function. Function names follow same rules as variables – parameter list: variables containing values passed to the function – body: statements that perform the function’s task, enclosed in {}
  • 8. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Function Definition Note: The line that reads int main()is the function header.
  • 9. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Function Return Type • If a function returns a value, the type of the value must be indicated: int main() • If a function does not return a value, its return type is void: void printHeading() { cout << "Monthly Salesn"; }
  • 10. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Calling a Function • To call a function, use the function name followed by () and ; printHeading(); • When called, program executes the body of the called function • After the function terminates, execution resumes in the calling function at point of call.
  • 11. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Functions in Program 4-1
  • 12. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Flow of Control in Program 4-1
  • 13. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Calling Functions • main can call any number of functions • Functions can call other functions • Compiler must know the following about a function before it is called: – name – return type – number of parameters – data type of each parameter
  • 14. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. 4.3 Function Prototypes
  • 15. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Function Prototypes • Ways to notify the compiler about a function before a call to the function: – Place function definition before calling function’s definition – Use a function prototype (function declaration) – like the function definition without the body • Header: void printHeading() • Prototype: void printHeading();
  • 16. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. (Program Continues)
  • 17. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Program 4-5 (Continued)
  • 18. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Prototype Notes • Place prototypes near top of program • Program must include either prototype or full function definition before any call to the function – compiler error otherwise • When using prototypes, can place function definitions in any order in source file
  • 19. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. 4.4 Sending Data into a Function
  • 20. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Sending Data into a Function • Can pass values into a function at time of call: c = pow(a, b); • Values passed to function are arguments • Variables in a function that hold the values passed as arguments are parameters
  • 21. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. A Function with a Parameter Variable void displayValue(int num) { cout << "The value is " << num << endl; } The integer variable num is a parameter. It accepts any integer value passed to the function.
  • 22. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. (Program Continues)
  • 23. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc.
  • 24. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. The function call in line 11 passes the value 5 as an argument to the function.
  • 25. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Other Parameter Terminology • A parameter can also be called a formal parameter or a formal argument • An argument can also be called an actual parameter or an actual argument
  • 26. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Parameters, Prototypes, and Function Headers • For each function argument, – the prototype must include the data type of each parameter inside its parentheses – the header must include a declaration for each parameter in its () void evenOrOdd(int); //prototype void evenOrOdd(int num) //header evenOrOdd(val); //call
  • 27. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Function Call Notes • Value of argument is copied into parameter when the function is called • A parameter’s scope is the function which uses it • Function can have multiple parameters • There must be a data type listed in the prototype () and an argument declaration in the function header () for each parameter • Arguments will be promoted/demoted as necessary to match parameters
  • 28. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Passing Multiple Arguments When calling a function and passing multiple arguments: – the number of arguments in the call must match the prototype and definition – the first argument will be used to initialize the first parameter, the second argument to initialize the second parameter, etc.
  • 29. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. (Program Continues)
  • 30. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Program 6-8 (Continued)
  • 31. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. The function call in line 18 passes value1, value2, and value3 as a arguments to the function.
  • 32. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. The return Statement • Used to end execution of a function • Can be placed anywhere in a function – Statements that follow the return statement will not be executed • Can be used to prevent abnormal termination of program • In a void function without a return statement, the function ends at its last }
  • 33. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. (Program Continues)
  • 34. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Program 6-11(Continued)
  • 35. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. 4.5 Returning a Value From a Function
  • 36. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Returning a Value From a Function • A function can return a value back to the statement that called the function. • You've already seen the pow function, which returns a value: double x; x = pow(2.0, 10.0);
  • 37. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Returning a Value From a Function • In a value-returning function, the return statement can be used to return a value from function to the point of call. Example: int sum(int num1, int num2) { double result; result = num1 + num2; return result; }
  • 38. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. A Value-Returning Function int sum(int num1, int num2) { double result; result = num1 + num2; return result; } Return Type Value Being Returned
  • 39. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. A Value-Returning Function int sum(int num1, int num2) { return num1 + num2; } Functions can return the values of expressions, such as num1 + num2
  • 40. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. (Program Continues)
  • 41. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Program 6-12 (Continued)
  • 42. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. The statement in line 17 calls the sum function, passing value1 and value2 as arguments. The return value is assigned to the total variable.
  • 43. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Another Example, from Program 6-13
  • 44. Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. Returning a Value From a Function • The prototype and the definition must indicate the data type of return value (not void) • Calling function should use return value: – assign it to a variable – send it to cout – use it in an expression