Download presentation
Presentation is loading. Please wait.
1
Value-Returning Functions
Tutorial 4 An Introduction to Programming with C++
2
An Introduction to Programming with C++
Objectives Create and invoke a function that returns a value Pass information, by value, to a function Understand a variable’s scope and lifetime Raise a number to a power using the Math::Pow() method Write a function prototype Format the numeric output in a C++ program An Introduction to Programming with C++
3
An Introduction to Programming with C++
Concept Lesson An Introduction to Programming with C++
4
An Introduction to Programming with C++
Functions 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 An Introduction to Programming with C++
5
Creating Value-Returning Functions
Value-returning function - returns a value after completing its assigned task Function header - the first line in a function definition Variable passed by value - only the value stored in the variable is passed to the function Variable passed by reference - the address of the variable in the computer’s internal memory is passed Formal parameter - each memory location listed in the parameterList An Introduction to Programming with C++
6
Syntax and an Example of a Value-Returning Function in C++
An Introduction to Programming with C++
7
An Introduction to Programming with C++
Function Body Contains the instructions the function must follow to perform its assigned task Last statement in the function body of a value-returning function is: return expression; An Introduction to Programming with C++
8
Processing a Function: Desk-checking Example 1
int main() { int number = 0; Console::Write(“Enter an integer: ”); number = Convert::ToInt32 (Console::Readline()); Console::WriteLine (calcCube(number)); return 0; } int calcCube (int num) return num * num * num; An Introduction to Programming with C++
9
Processing a Function: Desk-checking Example 1
An Introduction to Programming with C++
10
Processing a Function: Desk-checking Example 1
An Introduction to Programming with C++
11
Processing a Function: Desk-checking Example 1
An Introduction to Programming with C++
12
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) Local variables - variables declared within a function, and those that appear in a function’s parameterList An Introduction to Programming with C++
13
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
14
Processing a Function: Desk-checking Example 2
The following statements create and initialize two local variables: salesAmt and bonusAmt int salesAmt = 0; double bonusAmt = 0.0; Both variables: can be used only by the main() function remain in memory until the main() function’s return statement is processed An Introduction to Programming with C++
15
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
16
Processing a Function: Desk-checking Example 2
When salesAmt = getSales(); is encountered, the computer: temporarily leaves the main() function to process the instructions in the getSales() function getSales() function: Header does not contain formal parameters First statement creates and initializes local int variable, sales Next two statements prompt the user to enter a sales amount Response is stored in the sales variable An Introduction to Programming with C++
17
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
18
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
19
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
20
Processing a Function: Desk-checking Example 2
Computer temporarily leaves main() function to process code in the bonus() function parameterList in bonus() function: tells the computer to create two local variables - an int variable named dollars and a double variable named rate An Introduction to Programming with C++
21
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
22
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
23
Processing a Function: Desk-checking Example 2
An Introduction to Programming with C++
24
An Introduction to Programming with C++
Application Lesson An Introduction to Programming with C++
25
Analyzing, Planning, and Desk-checking the Periodic Payment Algorithm
Calculate the periodic payment on a loan for David Formula: Principal * rate/ (1 – ( rate+ 1)-term) Principal - the amount of the loan Rate - the periodic interest rate Term - the number of periodic payments An Introduction to Programming with C++
26
Periodic Payment Formula and Examples
An Introduction to Programming with C++
27
IPO Chart for the David Liu Problem
An Introduction to Programming with C++
28
An Introduction to Programming with C++
Revised IPO Chart According to the algorithm: The program asks the user to enter five input items It then converts the two annual rates to monthly rates, and the term, which is stated in years, to months The program then uses the periodic payment formula to calculate the two monthly car payments It then displays the result of both calculations on the screen An Introduction to Programming with C++
29
Revised IPO Chart for the Program’s main() Function
An Introduction to Programming with C++
30
IPO Chart for the calcPayment() Function
An Introduction to Programming with C++
31
Completed Desk-check Table
An Introduction to Programming with C++
32
Coding the main() Function
Determine how many memory locations the main() function needs to reserve Store values in variables An Introduction to Programming with C++
33
C++ Statements to Reserve Variables in the main() Function
An Introduction to Programming with C++
34
Coding the calcPayment() Function
calcPayment() function requires four memory locations: three for the input items and one for the output item. You need to list three variables in the calcPayment() function’s parameterList: an int variable to store the principal a double variable to store the monthly rate an int variable to store the number of months. An Introduction to Programming with C++
35
Coding the calcPayment() Function
An Introduction to Programming with C++
36
The Math::Pow() Method
Used to perform exponentiation Also returns result An Introduction to Programming with C++
37
Syntax and Examples of the Math::Pow() Method
An Introduction to Programming with C++
38
C++ Instructions for the calcPayment() Function
An Introduction to Programming with C++
39
Completing the David Liu Program
To open the partially completed C++ program: Start Microsoft Visual Studio .NET. If necessary, close the Start Page window Click File on the menu bar, and then click Open Solution Locate and then open the CppNet\Tut04\T4App Solution folder Click T4App Solution (T4App Solution.sln) in the list of filenames, and then click the Open button If the T4App.cpp source file is not displayed, right-click T4App.cpp in the Solution Explorer window, and then click Open An Introduction to Programming with C++
40
An Introduction to Programming with C++
Missing Instructions To enter the missing instructions, then test the program: Position the insertion point in the blank line below the //calculate monthly payments comment. First enter the two function calls Press Tab, if necessary, then type creditPay = calcPayment(carPrice - rebate, mthCreditRate, numMonths); and press Enter Type dealerPay = calcPayment(carPrice, mthDealerRate, numMonths); and press Enter. Next, enter the calcPayment() function definition Position the insertion point in the blank line below the //*****function definitions***** comment. First enter the function header An Introduction to Programming with C++
41
Missing Instructions (Cont.)
Type double calcPayment(int prin, double monthRate, int months) and press Enter. Next, enter the function body Save and build solution Double-click the first error message Save and then build the solution. The Task List window indicates that the compiler found errors in the program An Introduction to Programming with C++
42
Using a Function Prototype
Function Prototype - statement that specifies function’s name, data type of its return value (if any), and data type of each of its formal parameters Programs have one function prototype for each function defined below the main() function Function prototypes normally placed at the beginning of the program, after the #using and using directives An Introduction to Programming with C++
43
Syntax and an Example of a Function Prototype
An Introduction to Programming with C++
44
Formatting Numeric Output
Formatting - specifying the number of decimal places and the special characters to display in a number ToString( formatString): variablename is the name of a numeric variable formatString is a string that specifies the format you want to use formatString argument: takes the form Axx A is an alphabetic character called the format specifier xx is a sequence of digits called the precision specifier An Introduction to Programming with C++
45
Most Commonly Used Format Specifiers
An Introduction to Programming with C++
46
An Introduction to Programming with C++
Summary Functions: Code reuse Program modularity Value-returning versus void functions Pass information by value to a function Variable scope Local Global Variable lifetime An Introduction to Programming with C++
47
An Introduction to Programming with C++
Summary Function declaration/prototype Use of Math functions Format numeric output An Introduction to Programming with C++
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.