Professional Documents
Culture Documents
Department
Computer Programming
(MEng 1052)
Lecture 4
Functions
1
March, 2015
Introduction
A function is defined as a group of instruction used to achieve a
specific task.
When we write larger programs we have to have a way of
breaking problems down into smaller sub-problems.
C++, like most programming languages, has facilities to include
separate subparts inside of a program. In C++ these subparts are
called functions.
Functions decrease the complexity of a program. A program
having multiple functions can be easily debugged than a program
that is large and runs without a single function. So a function
reduces the size of a program and increases the program
modularity.
Functions would make the program easier to understand, write,
test, and debug.
Every C++ program has at least one function, main().
Introduction
Advantages of functions
Increase program modularity
Reduction in the amount of work & development time
Program and function debugging is easier
Division of work is simplified
reusability
Functions can be accessed repeatedly without
redevelopment, which in turn promotes reuse of code
Types of function
Functions are of two types:
Predefined functions (Library/built-in function)
2. User defined function
1.
Predefined functions
C++ comes with libraries of predefined functions that you can
Predefined functions
This enables the linker to find object code for the functions in
Predefined functions
Notice that the absolute value functions abs and labs are in the
library with header file cstdlib, so any program that uses either of
these functions must contain the following directive:
#include<cstdlib>
If you want to produce the absolute value of a number of type
int, you use abs; if you want to produce the absolute value of a
number of type long, you use labs; and if you want to produce
the absolute value of a number of type double, you use fabs.
abs and labs are in the library with header file cstdlib, while fabs
is in the library with header file cmath.
fabs is an abbreviation for floating-point absolute value.
Recall that numbers with a fraction after the decimal point, such
as numbers of type double, are often called floating-point
numbers.
10
Predefined functions
Another example of a predefined function is pow, which is
in the library with header file cmath. The function pow can
be used to do exponentiation in C++.
For example, if you want to set a variable result equal to xy,
you can use the following:
result = pow(x, y);
Hence, the following three lines of program code will
11
Function Call
A function call is an expression consisting of the function name
12
Example
13
14
PROGRAMMER-DEFINED FUNCTIONS
Declaring and Defining Functions
Using functions in your program requires that you first declare
15
Function Declaration
Function declarations are normally placed before the main part
of your program.
16
17
18
Function Declaration
The description of the function is given in two parts that are
Function Declaration
It tells you the name of the function, in this case total_cost.
It tells you how many arguments the function needs and
20
Function call
In the above sample program the function call is in the
following line:
bill = total_cost(number, price);
The function call is the expression on the right-hand side of
the equal sign.
The function name is total_cost, and there are two arguments:
The first argument is of type int, the second argument is of
type double.
The compiler does not care whether theres a comment along
with the function declaration, but you should always include a
comment that explains what value is returned by the function.
21
Function definition
A function definition describes how the function computes
Function definition
The function body consists of declarations and executable
23
Function definition
When this return statement is executed, the value of the
24
25
line 25. Note that the name, the return type, and the parameter
types are the same. If they were different, a compiler error
would have been generated.
In fact, the only required difference is that the function
prototype ends with a semicolon and has no body.
Also note that the parameter names in the prototype are length
and width, but the parameter names in the definition are l and w.
The names in the prototype are not used; they are there as
26
27
28
list with its prototype. It must provide names for all the
parameters, and the body of the function definition must be
surrounded by braces.
All statements within the body of the function must be
terminated with semicolons, but the function itself is not ended
with a semicolon; it ends with a closing brace.
If the function returns a value, it should end with a return
statement, although return statements can legally appear
anywhere in the body of the function. Every function has a
return type
29
30
Analysis
On lines 7 and 8, two float variables are declared, one to hold the
temperature in Fahrenheit and one to hold the temperature in
degrees Celsius.
The user is prompted to enter a Fahrenheit temperature on line
10, and that value is passed to the function Convert ( ).
Execution jumps to the first line of the function Convert ( ) on
line 20, where a local variable, also named TempCel, is declared.
Note that this local variable is not the same as the variable
TempCel on line 8. This variable exists only within the function
Convert ( ). The value passed as a parameter, TempFer, is also
just a local copy of the variable passed in by main ( ).
This function could have named the parameter FerTemp and the
local variable CelTemp, and the program would work equally
well. You can enter these names again and recompile the program
to see this work.
31
Analysis
The local function variable TempCel is assigned the value that
32
33
Local Variables
Variables that are declared within the body of a function
35
Global Variables
Variables defined outside of any function have global scope
and thus are available from any function in the program,
including main ( ).
Local variables with the same name as global variables do
not change the global variables. However, a local variable
with the same name as a global variable hides the global
variable.
If a function has a variable with the same name as a global
variable, the name refers to the local variable-not the global-when used within the function.
36
Example-Global variable
37
38
39
40
41
42
43
44
The following program finds the larger of two accepted numbers from
the user.
45
46
47
Exercise
48
Exercise
Write a function declaration and a function definition for a
function that takes three arguments, all of type int, and that
returns the sum of its three arguments.
49
Exercises
1.
2.
3.
4.
50
void FUNCTIONS
The functions discussed so far always return a single value.
In C++, a function must either return a single value or return
no values at all.
A function that returns no value is called a void function.
51
void FUNCTIONS
void FUNCTIONS
One difference is that we use the keyword void where we
53
void FUNCTIONS
54
55
56
57
58
59
Recursion Functions
Sometimes, the best way to solve a problem is by solving a
n!=
60
if n = 0
(n-1)!*n
if n > 0
if n = 0
1*2*3**(n-1)*n
if n > 0
(recursive solution)
Recursion Functions
61
62
63
Quiz_Sec. C (5%)
1. Write a function called isPrime ( ) that accepts a
number and determine whether the number is prime
or not.
2. Write a function called isEven ( ) that uses the
remainder operator(%) to determine whether an
integer is even or not.
64
Quiz_Sec. D (5%)
65
1.
2.
End of Lecture 4
Next Lecture
Lecture 5: Arrays and Strings
66