02 Lecture Two
02 Lecture Two
Review on Functions
2
3
4
Introduction
• What is a function?
– It is a small program with its own inputs, its own processing and its own
output.
This allows us to divide complicated tasks into smaller, simpler ones, and
drastically reduces the overall complexity of our program.
7
Introduction
• Why functions are useful?
2. Reusability:
Once a function is written, it can be called multiple times from within the
program.
Functions can also be shared with other programs, reducing the amount
of code that has to be written from scratch (and retested) each time.
8
Introduction
• Why functions are useful?
3. Testing:
Because functions reduce code redundancy, there’s less code to test in
the first place.
This reduces the amount of code we have to test at one time, making it
much easier to find bugs (or avoid them in the first place).
9
Introduction
10
Introduction
• Why functions are useful?
5. Abstraction:
In order to use a function, you only need to know its name, inputs,
outputs, and where it lives.
You don’t need to know how it works, or what other code it’s
dependent upon to use it.
12
Functions
• type is the data type specifier of the data returned by the function.
• name is the identifier by which it will be possible to call the function.
• parameters (as many as needed): Each parameter consists of a data
type specifier followed by an identifier, like any regular variable
declaration (for example: int x) and which acts within the function as a
regular local variable. They allow to pass arguments to the function when
it is called. The different parameters are separated by commas.
• statements are the function's body. It is a block of statements
surrounded by braces { }.
13
Function Example
The result is 8
// function example
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
14
Function Example
z = addition ( 5 , 3 );
15
Function Example
16
Variables Scope
• The scope of variables declared within a function or
any other inner block is only their own function or
their own block and cannot be used outside of them.
Global Variables
Local Variables
Instructions
18
Function Example
20
Arguments passed
by value
• When calling a function with parameters, what we
have passed to the function were copies of their
values but never the variables themselves.
Call by value 21
Arguments passed
by reference
x=2, y=6, z=14
// passing parameters by reference
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
22
Arguments passed
by reference
duplicate ( x , y , z );
23
Arguments passed
by value
duplicate ( x , y , z );
24
Passing by reference
allows a function to return
more than one value
Previous=99, Next=101
// more than one returning value
#include <iostream>
using namespace std;
void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}
25
Default values in
parameters
• When declaring a function we can specify a default
value for each of the last parameters.
26
Default values in
parameters
6
// default values in functions 5
#include <iostream>
using namespace std;
int divide (int a, int b=2)
{
int r;
r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
27
Overloaded
Functions
• Two different functions can have the same name if
their parameter types or number are different.