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

02 Lecture Two

Uploaded by

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

02 Lecture Two

Uploaded by

nouryones38
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

By

Dr. Eman Monir


Lecturer, Computer Science Department,
Faculty of Computers & Artificial Intelligence,
Benha University Lecture 2
1
Lecture Two

Review on Functions

2
Introduction
• What is a function?
– It is a small program with its own inputs, its own processing and its
own
output.

• Why we need to use a function in a program?


1. Functions allow divide and conquer strategy: When developing large
programs, it’s difficult to write entire program under single large main()
program. These programs are difficult to test and debug and maintain.
Therefore, the task to be performed is normally divided into several
independent sub tasks thereby reducing overall complexity.

2. Functions avoid duplication: Code that appears more than once in a


program should generally be made into a function.
3
Introduction
• Why we need to use a function in a program?
3. Hide the implementation details: Function enable us to
hide the implementation details, e.g. we are using sqrt(),
log(), sin() etc. without knowing their implementation.

4. The function developed for one program can be reused


by another with small or no modification. This further
leads to reduction in development time and cost of
program.
4
Introduction
• Why functions are useful?
1. Organization:
As programs grow in complexity, having all the code live inside the main()
function becomes increasingly complicated.

A function is almost like a mini-program that we can write separately


from the main program, without having to think about the rest of the
program while we write it.

This allows us to divide complicated tasks into smaller, simpler ones, and
drastically reduces the overall complexity of our program.

5
Introduction
• Why functions are useful?
2. Reusability:
Once a function is written, it can be called multiple times from within the
program.

This avoids duplicated code and minimizes the probability of copy/paste


errors.

Functions can also be shared with other programs, reducing the amount
of code that has to be written from scratch (and retested) each time.

6
Introduction
• Why functions are useful?
3. Testing:
Because functions reduce code redundancy, there’s less code to test in
the first place.

Also because functions are self-contained, once we’ve tested a


function
to ensure it works, we don’t need to test it again unless we change it.

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).
7
Introduction

• Why functions are useful?


4. Extensibility:
When we need to extend our program to handle a
case it didn’t handle before, functions allow us to
make the change in one place and have that change
take effect every time the function is called.

8
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.

This is super-useful for making other people’s code accessible


(such as everything in the standard library).
9
Functions

• A function is a group of statements that is executed


when it is called from some point of the program.

type name ( parameter1, parameter2, ...)


{
statements
}

10
Functions

type name ( parameter1, parameter2, ...) { statements }

• 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 { }.
11
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;
} 12
Function Example

int addition (int a, int b)

z = addition ( 5 , 3 );

13
Function Example

int addition (int a, int b


8
z = addition ( 5 , 3
);

14
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.

• For example, in the previous example it would have


been impossible to use the variables a, b or r directly
in function main since they were variables local to
function addition.
15
Variables Scope

Global Variables

Local Variables

Instructions

16
Function Example

// function example The first result is 5


#include <iostream> The second result is 5
using namespace std;
The third result is 2
int subtraction (int
a, int b) The fourth result is 6
{
int r;
r=a-
b;
return
(r);
}
int main ()
{
int
x=5,
y=3,
z;
z =
subtra 19
ction
Functions with no
type: The use of
void I'm a function!
// void function example
#include <iostream>
using namespace std;
void printmessage ()
{
cout << "I'm a
function!";
}
int main ()
{
printmessage ();
return 0;
}

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.

int x=5, y=3, z; int addition (int a, int b)


5
z = addition ( x , y );
3
z = addition ( x value
Call by , y );
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; 20
Arguments passed
by reference

void duplicate (int& a, int& b, int& c)


x y z

duplicate ( y , z
x );
,
21
Arguments passed
by value

void duplicate (int a, int b, int c)


1
3 7

duplicate ( x , y , z
);
22
Passing by reference
allows a function to
return more than one
Previous=99, Next=101value
// 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.

• This value will be used if the corresponding argument


is left blank when calling to the function.

24
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;
}

25
Overloaded

Functions
• Two different functions can have the same name if
their parameter types or number are different.

• That means that you can give the same name to


more than one function if they have either a different
number of parameters or different types in their
parameters.
26
Overloaded

// overloaded function
#include <iostream>
10
2.5
Functions
using namespace std;
int operate (int a,
int b)
{
return (a*b);
}
float operate (float
a, float b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m); 29
cout << "\n";
Declaring functions
// declaring functions prototypes
#include <iostream> using namespace std;
void odd (int a); void even (int a);
int main ()
{ Type a number (0 to exit): 9
int i; do {
cout << "Type a number (0 to exit): Number is odd.
";
cin >> i; Type a number (0 to exit): 6
odd (i); Number is even.
} while
(i!=0); Type a number (0 to exit):
return 0;
} 1030 Number is even.
void odd Type a number (0 to exit): 0
(int a) Number is even.
{
if ((a
%2)!=0)
cout << "Number is odd.\n";
else
even (a);
}
30
void even (int a)
31

You might also like