0% found this document useful (0 votes)
78 views

Chapter 6: User-Defined Functions I: Programming For Engineers 1

This document discusses user-defined functions in C++. It explains that functions allow complex programs to be divided into manageable pieces. User-defined functions are created by the programmer and allow problems to be broken down into subproblems. The document provides examples of defining, calling, and using value-returning user-defined functions in C++ programs.

Uploaded by

AbdelrahmanJamal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

Chapter 6: User-Defined Functions I: Programming For Engineers 1

This document discusses user-defined functions in C++. It explains that functions allow complex programs to be divided into manageable pieces. User-defined functions are created by the programmer and allow problems to be broken down into subproblems. The document provides examples of defining, calling, and using value-returning user-defined functions in C++ programs.

Uploaded by

AbdelrahmanJamal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 6: User-Defined

Functions I

Programming for Engineers

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

Programming for Engineers

Functions (Continued)
problem
Subprobelm1

Subprobelm1.1

Subprobelm2

Subprobelm3

Subprobelm1.2

Subprobelm3.1

Programming for Engineers

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

Programming for Engineers

Functions (Continued)
Functions
Called modules
Like miniature programs
Can be put together to form a larger program

Programming for Engineers

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

Predefined Functions (continued)


Some of the predefined mathematical functions
are:
sqrt(x)
pow(x,y)
floor(x)

Programming for Engineers

Predefined Functions (continued) The Power Function (pow)


pow(x,y) calculates xy, pow(2,3) = 8.0
pow returns a value of the type double
x and y are called the parameters (or
arguments) of the function pow. Both are
of type double.
Function pow has two parameters
Programming for Engineers

Predefined Functions (continued) The sqrt and floor Functions


The square root function sqrt(x)
Calculates the non-negative square root of x,
for x >= 0.0
sqrt(2.25) is 1.5
Type double and has only one double
parameter

Programming for Engineers

10

Predefined Functions (continued) The sqrt and floor Functions (continued)


The floor function floor(x)
Calculates largest whole number not greater
than x
floor(48.79) is 48.0
Type double and has only one double
parameter
Programming for Engineers

11

Programming for Engineers

12

Predefined Functions (continued)


#include <iostream>
#include <cmath>
using namespace std;

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

Predefined Functions (continued)


Predefined functions are organized into separate
libraries.
To use a predefined function, the program must
include the header file that contains the
functions specifications.
To use the predefined math functions sqrt(),
pow(), must have #include <cmath>
To use the I/O functions, must have
#include <iostream>
Example of another header file that you have
seen so far is .
Programming for Engineers

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

Programming for Engineers

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

double larger(double x, double y)

Function formal
parameters

largest = larger(num1, num2);


cout << "The largest is " << largest << endl;
return 0;

Function actual
parameters

Function
header/heading
Function body

double max;
if (x >= y)
max = x;
else
max = y;
return max;

Function definition

Programming for Engineers

17

User-Defined Functions (Continued)


The number of formal parameter must
match the number of actual parameters
The data type of each actual parameter
must match its corresponding formal
parameter.
A function call in a program results in
the execution of the body of the called
function
Programming for Engineers

18

User-Defined Functions
(continued)
The syntax is:
functionType functionName(formal parameter list)
{
statements
}

functionType: type of the value returned by


the function
Also called the function data type or the return
type.
Programming for Engineers

19

User-Defined Functions (Continued)


The formal parameter list can be empty
If the formal parameter list is empty
Parentheses are still needed
Function heading of the function takes either
of the following forms:
functionType functionName()
functionType functionName(void)

In a function call the actual parameter is


empty

Programming for Engineers

20

User-Defined Functions (Continued)


Function Prototype
Function Prototype: function heading without
the body of the function
See example on slide 16
It is not necessary to specify the variable
name in the parameter list
double larger(double , double );
The data type of each parameter must be
specified
Programming for Engineers

21

User-Defined Functions (Continued)


Flow of Execution
A C++ program is made out of a set of
function
main() is just a user defined function
Execution always begins at the first statement
in the function main.
Other functions are executed only when they
are called
Programming for Engineers

22

User-Defined Functions (Continued)


Flow of Execution -The return Statement
When a return statement executes
Function immediately terminates
Control goes back to the caller
The function call statement is replaced by
the returned value.

When a return statement executes in the


function main(), the program terminates

Programming for Engineers

23

User-Defined Functions (Continued)


Flow of Execution
A user-defined function can be placed before
the function main. In that case the function
prototype is not necessary.

Programming for Engineers

24

User-Defined Functions (Continued)


Void Functions
A function that returns a value is called a
value-returning functions and it must have a
data type.
A function that does not return a value (i.e.
does not have a return statement) is called a
void function and it does not have a type (its
type is void).
The prototype of a function that do not return a
value is:

void function_name ( parameter_list);


Programming for Engineers

25

Programming Example
isPerfect()

with print() in main()

isPrime() with print() in main()


isArmestrong() with print() in main()
Fibonacci()
n!
C
k! (n k )!
n
k

Programming for Engineers

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

Program Analysis (continued)


Now max contains the larger of the first two
numbers
Read the third number and compare it with
max and store the larger number into max
At this point, max contains the largest of the
first three numbers
Read the next number, compare it with max,
and store the larger into max
Repeat this process for each remaining
number in the data set
Programming for Engineers

29

Algorithm Design (continued)


Main Algorithm

1). Get number


2). Set maxSoFar= number
3). While not all numbers checked
a). Get the next number
b). Call the function larger to find the larger value of the
maxSoFar and number, and set maxSoFar to that value.
4). Output maximum
Function larger(n1, n2); Returns the larger of n1 and n2

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

You might also like