Académique Documents
Professionnel Documents
Culture Documents
Functions
LESSON OUTCOMES
Students will be able to :
implement functions
describe the concept of parameter passing
develop strategies for decomposing
complex tasks into simpler ones
determine the scope of a variable
recognize when to use value and reference
parameters
INTRODUCTION TO
FUNCTION
To apply the top-down design, the programmer starts with the broadest
statement of the problem solution and works down a more detailed sub-problems
For example, we are to provide a program for finding the area and circumference
of a circle. Then, top-down design can break the solution into 3 parts such as: (1)
get input, (2) calculate area & circumference and (3) display results.
Next, each part will be further refined until it can be implemented using certain
programming code.
CONTD
Circle
program
Get Input
Calculate area
Display
results
Calculate
circumference
CONTD
CONTD
Non-function
Using function
PREDEFINED FUNCTION
CONTD
To use these pre-defined functions we must:
(1) Include the appropriate header file in the program,
e.g., to use pow function, need to #include <cmath>
USER-DEFINED FUNCTIONS
updateAccount(200-0100,3000.00)
printSquare(4)
toggleStatus()
isBigger(x,5)
FUNCTION INVOCATION
11
CALLING A FUNCTION
A programmer
calls a function
to have its
instructions
executed.
the caller
(getting things done)
the function
(has the modify temperature instructions)
12
CALLING A FUNCTION
Execution flow
during a
function call
13
CONTD
#include <iostream>
using namespace std;
int getRadius () {
int radius;
cout << "Enter radius: ";
cin >> radius;
return radius;
}
void calculateCircle (double &area, double &perimeter, int radius){
area = 3.1415 + radius * radius;
perimeter = 2 * 3.1415 * radius;
}
void displayCircle(double area, double perimeter, int radius) {
cout << "Area of circle with radius " << radius << " ==> " << area << endl;
cout << "Perimeter of circle with radius " << radius << " ==> " <<
perimeter << endl;
}
TYPES OF FUNCTIONS
There are 2 types of functions:
17
IMPLEMENTING
FUNCTIONS
CONTD
19
CONTD
20
PARAMETERS
int main()
{
double z = pow(2, 3);
...
When another function
calls the pow function, it provides
inputs, such as the values 2 and 3 in the call pow(2, 3).
}
In order to avoid confusion with inputs that are provided
by a human user (cin >>), these values are called
parameter values.
The output that the pow function computes is called the
return value (not output using <<).
21
return Statement
Once a value-returning function computes the value,
the function returns this value via the return
statement
It passes this value outside the function via the
return statement
22
23
Value-Returning Functions
Function header
Example:
float square( int y, float z)
type var_1, type var_2,
{
return y * z;
}
24
Value-Returning Functions
1
int main(){
int a = add(3,4);
cout << a;
Passing
arguments
int main()
{
cout << add(3,4);
return 0;
}
return 0; }
Return
value
return b+c;
25
Value-Returning Functions
3
int add(int b, int c);
int main()
{
int b=3, c=4;
cout << add(b,c);
return 0;
}
int main()
{
cout << add(3,4);
return 0;
}
26
Value-Returning Functions
5
6
int add(int b, int c)
{
return b+c;
}
int main()
{
int b=3, c=4;
cout << add(b,c);
return 0;
}
27
28
29
30
31
Void Functions
void box_string(string str)
{
int n = str.length();
for (int i = 0; i < n + 2; i++){ cout << "-"; }
cout << endl;
cout << "!" << str << "!" << endl;
for (int i = 0; i < n + 2; i++) { cout << "-"; }
cout << endl;
}
#include <iostream>
using namespace std;
void prt (int);
//Function prototype
int main ( )
{
int X = 12; //Defines the integer variable
prt ( X ); //Calls prt ( ) and passes it X
return 0;
// Returns 0 to the environment
}
Passing
arguments
void add(int,int,float);
int main()
{
add(3, 1, 2.3);
return 0;
}
34
VARIABLE SCOPE
35
VARIABLE SCOPE
36
VARIABLE SCOPE
double cube_volume(double side_len)
{
double volume = side_len * side_len * side_len;
return volume;
}
int main()
{
double volume = cube_volume(2);
cout << volume << endl;
return 0;
}
VARIABLE SCOPE
38
40
GLOBAL VARIABLES
GLOBAL VARIABLES
In some cases, this is a good thing:
The <iostream> header defines these global
variables:
cin
cout
This is good because there should only be one of
each of these and every function who needs them should
have direct access to them.
42
GLOBAL VARIABLES
int balance = 10000; // A global variable
void withdraw(double amount)
{
if (balance >= amount)
{
balance = balance - amount;
}
}
int main()
{
withdraw(1000);
cout << balance << endl;
return 0;
}
When multiple
functions update
global variables,
the result can be
difficult to predict.
43
avoid
You should
global
variables in your programs!
44
Call by reference
Function can directly access data
45
int main()
{
int id;
string name;
getData(id,name);
46
int main()
}
{
int a = 10, b = 20;
swap(a,b);
cout << "Value of a is " << a <<endl;
cout << "Value of b is " << b << endl;
return 0;
}
47
Exercises
Consider the following function scramble():
void scramble (int i, int &j, int &k) {
i = 10; j = 20; k = 30;
}
int main() {
int i=1, j=2, k=3;
scramble(i, j, k);
cout << i= << i << j=
<< j
<< k= << k <<
endl;
return 0;
}
#include <iostream>
int main() {
int i=1, j=2, k=3;
scramble(j, j, j);
cout << i= << i << j=
<< j
<< k= << k <<
endl;
return 0;
48
}
Exercises
What is the output of the following C++ program?
#include <iostream>
using namespace std;
int funny (int &a, int b) {
int c = a + b;
a = b; b = c;
return c; }
int main() {
int x = 3, y = 4, z = 5;
cout << funny(x, y) << endl;
cout << x= << x << y= << y << endl;
z = funny(x, z);
cout << z= << z << x= << x << endl;
return 0;
}
49
Exercises
Consider the following valid program:
#include <iostream>
using namespace std;
int main() {
int x = 1;
f(x);
cout << x= << x << endl;
return 0;
}
SUMMARY
Function Definitions
Function Prototypes
Function Calls
51