UserDefined Functions Read Malik p ages 345382 in Chapter 6 Homework 6 and Lab 6 due next week Quiz next week Introduction Functions are like miniature programs that can be combined to form larger programs ID: 554561
Download Presentation The PPT/PDF document "EGR 2261 Unit 6" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
EGR 2261 Unit 6User-Defined Functions
Read Malik,
p
ages
345-382 in Chapter 6
.
Homework #6 and Lab #6 due next week.
Quiz next week (covers Units 5 and 6).Slide2
Introduction
Functions
are like miniature programs that can be combined to form larger programs.They let you divide complicated programs into manageable pieces. Advantages of this approach include:It’s easier to test small pieces.You may be able to reuse pieces in later projects.Functions are sometimes called subroutines, subprograms, or modules.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
2Slide3
Predefined Functions
The textbook distinguishes
predefined functions
(which someone else has written for us) from user-defined functions (which we write ourselves).As you know, predefined functions are organized into library files that you can include in your programs. For example:I/O functions are in <iostream>.Math functions are in <
cmath>.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
3Slide4
Predefined Functions (cont'd.)
Many predefined functions require one or more
parameters
(input values that you must provide when you call the function). Another name for parameters is arguments.Examples of some predefined functions: rand() takes no parameters.
sqrt(2.5) takes one parameter. pow(x, y) takes two parameters.See Table 6-1 in the text (next two slides) for some more predefined functions.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
4Slide5
Some Predefined Functions
Continued on next slide….Slide6
Some Predefined FunctionsSlide7
Using a Predefined Function
To use a predefined function, you must:
Include the appropriate header file in your program using the
#include directive.Know the following items:Name of the function.Number of parameters, if any.Data type of each parameter.Data type of the value returned (if any): we call this the function’s type or return type.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
7Slide8
Here is the information you need to know to use the power and square root functions.
Examples
Power
Square Root
Function namepowsqrtHeader file <
cmath
>
<
cmath
>
Number of parameters
2
1
Data types of parameters
double, double
double
Return type
double
doubleSlide9
Here are several attempts to call the
sqrt
function. Most of these fail for the reasons noted in the comments.
Examples of Errors When Calling a FunctionSlide10
Value-Returning Functions versus Void Functions
Every function is either value-returning or void.
Value-returning functions
have a return type.When the execute, they return a value of a specific data type.Void functions do not have a return type.When they execute, they do not return a value.All of the functions listed in Table 6-1 are value-returning functions.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
10Slide11
Value-Returning Functions
Here are some
ways
you can use the value returned by a value-returning function:You can display it: cout << pow(4, 3);You can assign it to a variable for later use.
myResult = pow(4, 3);You can use it as part of a larger expression. if (pow(4, 3) + x > 200)Common mistake: “Drop the ball” by not doing anything with the return value: pow(4, 3);
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
11Slide12
User-Defined (Programmer-Defined) Functions
Our textbook uses the term “user-defined function” to refer to functions that we create when we write our programs.
This is in contrast to predefined functions.
A better term might be “programmer-defined function,” since the user is the person who will be using our programs after we write them. That person doesn’t define functions—rather, we as programmers do.
12Slide13
How Many User-Defined Functions in a Program?
Every C++ program must contain at least one user-defined function, which must be named
main
, whose return type must be int.That’s why all of our programs contain a line like this: int main() {A program can also contain as many other user-defined functions as you wish.
13Slide14
Flow of Execution
When a program runs, execution always begins at the first statement in the function named
main.
Other functions are executed only when called.A function call transfers control to the first statement in the body of the called function.When the called function ends, control is passed back to the point immediately following the function call.The function’s returned value (if any) replaces the function call.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
14Slide15
Example of a Program with Two User-Defined Functions
Function call
Function definition
Execution starts here, even though the other function is listed first.Slide16
Single-Stepping: F10 versus F11
You know that F10 (or
Debug > Step Over
) lets you single-step a program. When you’re single-stepping and you reach a statement that calls a function, you have a choice:
Use F10 if you want to execute the entire function in one step. Use F11 (or Debug > Step Into) to step into the function and execute each of its statements one at a time.If you’ve stepped into a function and want to execute the rest of it as one step, use Shift+F11 (or Debug > Step Out).Slide17
Syntax: Value-Returning Function
Syntax to
define
a value-returning function:The formal parameter list can be empty:Or it can consist of a list of comma-separated dataTypes and identifiers:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
17
functionType
functionName
(
dataType
identifier,
dataType
identifier, …)Slide18
Some Terminology
A function’s
heading
(or function header) is the first line of the function.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
18Slide19
Function Definition versus Function Calls
For a user-defined function other than main(), you
define
the function once
, and you call the function as many times as you wish.Function callsFunction definitionSlide20
Formal Parameters versus Actual Parameters
A
formal parameter
is a variable declared in a function heading. Example: double addOne(double numberIn)
An actual parameter is an expression or variable listed in a call to a function.Examples: modified = addOne(original); cout << addOne(156.7);
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
20Slide21
Function Call
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
Syntax to
call
a function:
Syntax of the actual parameter list
:
If the function’s formal parameter list is empty, a call to the function looks like:
21Slide22
Example of a Function with No Parameters
Call to a function with no parameters
Definition of a function with no parametersSlide23
return Statement
A value-returning function returns its value via the
return
statement.It passes this value outside the function to the statement that called it.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
23Slide24
Syntax: return Statement
Syntax:
When a
return statement executes:The function immediately terminates.Control goes back to the caller and the function call in the calling statement is replaced by the function’s return value.When a return statement executes in the function main, the program terminates.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
24Slide25
More Than One return
Statement?
A function can have more than one
return statement. On any particular function call, only one of these will be executed.Example:
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
25Slide26
Caution #1: Make Sure that Value-Returning Functions Always Execute a Return
If the
return
statements in a function are located inside if statements or other conditional code, your function may not execute any return statements when it runs.This is almost always a mistake that you want to avoid.See example on next slide.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
26Slide27
Caution #1: Make Sure that Value-Returning Functions Always Execute a Return
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
27
In this code we’re guaranteed to execute a
return
for every possible value of
x
.
In this code we won’t execute a
return
for some values of
x
.Slide28
Caution #2: A Value-Returning Function Can Only Return One Value
Although you can pass as many values as you want
into
a function (via the parameter list), you can only return one value from a function (via a return statement).See next slide for two examples of functions that try (but fail) to return more than one value.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
28Slide29
Caution #2: A Value-Returning Function Can Only Return One Value
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
29Slide30
Summary: Parts of a User-Defined Function
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
30Slide31
Nested Function Definitions Not Allowed
Recall that you can nest loops within other loops,
if
statements within other if statements, loops within if statements, etc.You can also call one function from within another function.But in C++ you cannot define one function within another function. See example on next slide…
31Slide32
Example: Nested Functions Not Allowed
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
32Slide33
Function Prototype
User-defined functions can be placed either before or after the function
main
.Although execution always starts with main, compilation proceeds from top to bottom.So if a user-defined function is placed after the function main, the function’s prototype must be placed before the function
main. The compiler can then correctly translate a function call.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
33Slide34
Function Prototype (cont’d.)
A
function prototype
is a copy of the function heading, ending in a semicolon.Syntax:In the parameter list you must at least specify each parameter’s data type. You don’t have to specify each parameter’s name, but you can.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
34Slide35
Function Prototype (cont’d.)
Example: Consider this function definition:
double
addOne(double numberIn)
{ double numberOut; numberOut = numberIn + 1; return numberOut;
}
Here are two acceptable function prototypes:
double
addOne
(double
numberIn
);
double
addOne
(double);
The following is not an acceptable prototype:
double
addOne
();
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
35Slide36
Review: Value-Returning Functions versus Void Functions
Recall that every function is either value-returning or void.
Value-returning functions
have a return type.They return a value of a specific data type using one or more return statements.Void functions do not have a return type.They may contain one or more return statements, but these return statements cannot return values.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
36Slide37
Void Functions (cont’d.)
Void function definition syntax:
As
with value-returning functions, a void function might not take any formal parameters, in which case the formal parameter list is empty.
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
37Slide38
Summary: Several Possibilities
A function’s heading tells you whether the function takes parameters and whether it returns a value. We have at least four possibilities:
Doesn’t take parameters, doesn’t return a value:
void myFunc1()Takes parameters, doesn’t return a value:
void myFunc2(int num1, double z)Doesn’t take parameters, returns a value: double myFunc3()Takes parameters, returns a value: int myFunc4(double sum, bool finished)
38Slide39
Void Functions (cont’d.)
Function call
syntax for a void function:
A call to a void function is a stand-alone statement. You can’t use it as part of an assignment statement or as part of a larger expression, as we did with value-returning functions in these earlier examples: cout << pow(4, 3);
myResult = pow(4, 3); if (pow(4, 3) + x > 200)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition
39Slide40
Example of a Void Function
This
return
statement does not return a value.
This function call is a standalone statement, not part of a bigger statement.