Download presentation
Presentation is loading. Please wait.
1
Chapter 9: Value-Returning Functions
Introduction to Programming with C++ Fourth Edition
2
Objectives Raise a number to a power Generate random numbers
Create and invoke a function that returns a value Pass information, by value, to a function Write a function prototype Understand a variable’s scope and lifetime Introduction to Programming with C++, Fourth Edition
3
Functions Functions: Value returning functions return a value
Allow programmers to avoid duplicating code in different parts of a program Allow large and complex programs to be broken into small and manageable tasks Value returning functions return a value Void functions do not return a value Introduction to Programming with C++, Fourth Edition
4
Raising a Number to a Power
pow() function: Raises a number to a power Returns the result as a double number Is used to perform exponentiation Program must contain the #include <cmath> directive and the using std::pow; statement Introduction to Programming with C++, Fourth Edition
5
Syntax and Examples of the pow() Function
Introduction to Programming with C++, Fourth Edition
6
Generating Random Numbers
Most programming languages have a built-in function for generating random numbers In C++, the random number generator is the rand() function It returns an integer that is greater than or equal to 0, but less than or equal to RAND_MAX You must initialize the random number generator in each program in which it is used You initialize the random number generator using the srand() function Introduction to Programming with C++, Fourth Edition
7
Generating Random Numbers (continued)
Introduction to Programming with C++, Fourth Edition
8
Generating Random Numbers (continued)
Introduction to Programming with C++, Fourth Edition
9
Generating Random Numbers (continued)
Introduction to Programming with C++, Fourth Edition
10
Creating Program-Defined Value-Returning Functions
Introduction to Programming with C++, Fourth Edition
11
Function Header The function header is the first line in a function definition It specifies: The type of data the function returns (if any) The function’s name An optional parameterList Introduction to Programming with C++, Fourth Edition
12
Function Header (continued)
Items of information you send (pass) to a function are called actual arguments When a variable is passed by value, only the value stored in the variable is passed When a variable is passed by reference, the address of the variable in memory is passed Variables in C++ are automatically passed by value Introduction to Programming with C++, Fourth Edition
13
Function Header (continued)
Each memory location listed in the parameterList is referred to as a formal parameter The number of formal parameters included in the parameterList should agree with the number of actual arguments passed to the function The data type and position of each formal parameter in the parameterList must agree with the data type and position of its corresponding actual argument in the argumentList Introduction to Programming with C++, Fourth Edition
14
Function Body The function body:
Contains the instructions the function must follow to perform its assigned task Begins with the opening brace ({) and ends with the closing brace (}) Last statement of a value-returning function is: return expression; return statement alerts the computer that the function has completed its task, and it ends the function Introduction to Programming with C++, Fourth Edition
15
Using a Function Prototype
Function prototype - a statement that specifies the function’s name, the data type of its return value (if any), and the data type of each of its formal parameters Programs have one function prototype for each function defined below the main() function Function prototypes are normally placed at the beginning of the program, after the #using and using directives Introduction to Programming with C++, Fourth Edition
16
Processing a Function Call a program-defined function the same way as you call a built-in function, by including its name and actual arguments (if any) in a statement If a function call has an argumentList, the values of the actual arguments are passed (assuming passed by value) to the called function Function receives these values and stores them in the memory locations listed in parameterList Introduction to Programming with C++, Fourth Edition
17
Processing a Function (continued)
Introduction to Programming with C++, Fourth Edition
18
The Scope and Lifetime of a Variable
Variable’s scope - indicates which portions of a program can use the variable (can be either local or global) Variable’s lifetime - indicates how long the variable remains in the computer’s memory Local variables - variables declared in a function, and those that appear in a function’s parameterList Introduction to Programming with C++, Fourth Edition
19
The Scope and Lifetime of a Variable (continued)
Global variables: Are declared outside of any function Remain in memory until the program ends Can be used by any statement in the program Global variables allow unintentional errors to occur when a function that should not have access to the variable inadvertently changes it Avoid using global variables in your programs Introduction to Programming with C++, Fourth Edition
20
Summary Functions allow: Math functions include:
Large and complex programs to be broken into small and manageable tasks Code to be reused Math functions include: Raising a number to a power The random number generator Introduction to Programming with C++, Fourth Edition
21
Summary (continued) Value returning functions return a value
Void functions do not return a value Pass information by value to a function Variables can be either local or global in scope Introduction to Programming with C++, Fourth Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.