Chapter 6: User-Defined Functions I: Programming For Engineers 1
Chapter 6: User-Defined Functions I: Programming For Engineers 1
Functions I
Objectives
In this chapter you will:
Learn about standard (predefined) functions
and discover how to use them in a program
Learn about user-defined functions
Examine value-returning functions, including
actual and formal parameters
Explore how to construct and use a valuereturning, user-defined function in a program
Programming for Engineers
Functions
Example: solve a second degree equation
Divide into three sub problems:
Get the equation
Calculate the determinant
Calculate the roots
Functions (Continued)
problem
Subprobelm1
Subprobelm1.1
Subprobelm2
Subprobelm3
Subprobelm1.2
Subprobelm3.1
Subprobelm3.2 Subprobelm3.3
Functions (Continued)
Functions are like building blocks
They allow complicated programs to be divided
into manageable pieces
Some advantages of functions:
A programmer can focus on just that part of the
program and construct it, debug it, and perfect it
Different people can work on different functions
simultaneously
Can be used in more than one place in a
program or in different programs
Functions (Continued)
Functions
Called modules
Like miniature programs
Can be put together to form a larger program
Predefined Functions
In algebra, a function is defined as a rule or
correspondence between values, called the
functions arguments, and the unique value of
the function associated with the arguments
If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and
f(3) = 11
1, 2, and 3 are arguments
7, 9, and 11 are the corresponding values
Programming for Engineers
10
11
12
Function call
int main() {
double u, v, uToPowerv;
u= 4.2;
v= 3.0;
uToPowerv = pow(u,v);
cout << "u to the power v = " << uToPowerv << endl;
cout << "u to the power v = " << pow(u,v) << endl;
u = u + pow(3, 3);
Must include <cmath>
cout << "u = " << u << endl;
pow()
return 0;
}
Can be assigned to a variable
can be used in an expression
or in an outupt statement.
Programming for Engineers
13
14
User-Defined Functions
User defined functions are like predefined
functions but the programmer has to write
them.
Example:
Find the max of two numbers.
Write pseudocode first.
C++ code on next slide
15
#include <iostream>
using namespace std;
double larger(double x, double y);
int main()
{
double num1, num2, maximum;
cin >> num1 >> num2;
maximum = larger(num1, num2);
cout << "The maiximum is " << maximum << endl;
return 0;
}
double larger(double x, double y)
{
double max;
if (x >= y)
max = x;
else
max = y;
return max;
}
Programming for Engineers
16
#include <iostream>
using namespace std;
double larger(double x, double y);
Function prototype
int main()
{
double num1, num2, largest;
cin >> num1 >> num2;
Function
type
Function formal
parameters
Function actual
parameters
Function
header/heading
Function body
double max;
if (x >= y)
max = x;
else
max = y;
return max;
Function definition
17
18
User-Defined Functions
(continued)
The syntax is:
functionType functionName(formal parameter list)
{
statements
}
19
20
21
22
23
24
25
Programming Example
isPerfect()
26
Programming Example
In this programming example, the function
larger is used to determine the largest
number from a set of numbers
Program determines the largest number from
a set of 10 numbers
Input: A set of 10 numbers
Output: The largest of 10 numbers
Programming for Engineers
27
Program Analysis
Suppose that the input data is:
15 20 7 8 28 21 43 12 35 3
Read the first number of the data set
Because this is the only number read to this
point, you may assume that it is the largest
number so far and call it max
Read the second number and call it num
Compare max and num, and store the larger
number into max
Programming for Engineers
28
29
If n1 >= n2
the larger is n1, return n1
Otherwise
the larger is n2, return n2
Programming for Engineers
30
Summary
Functions (modules) are miniature programs
Functions enable you to divide a program into
manageable tasks
C++ provides the standard functions
Two types of user-defined functions: valuereturning functions and void functions
Variables defined in a function heading are
called formal parameters
Expressions, variables, or constant values in a
function call are called actual parameters
Programming for Engineers
31
Summary
In a function call, the number of actual
parameters and their types must match with the
formal parameters in the order given
To call a function, use its name together with the
actual parameter list
Function heading and the body of the function
are called the definition of the function
If a function has no parameters, you need
empty parentheses in heading and call
A value-returning function returns its value via
the return statement
Programming for Engineers
32
Summary
A prototype is the function heading without the
body of the function; prototypes end with the
semicolon
Prototypes are placed before every function
definition, including main
User-defined functions execute only when they
are called
In a call statement, specify only the actual
parameters, not their data types
Programming for Engineers
33