You are on page 1of 7

Algorithm Design and Programming Techniques Lecture 6

1st class Asst. Lecture Omar Nowfal

Passing by Reference:
 When we need the function returns more than one value, we can pass arguments by
reference to the function.
 A reference provides an alias—another name—for a variable (It is actually the memory
address of the variable that is passed).
 An important advantage of passing by reference is that the function can access the actual
variables in the calling function. This provides a mechanism for passing more than one
value from the function back to the calling function.

Example: Simple algorithm to understand the calling by reference:


Algorithm Main( )
Begin
define y ← 10
define &w ← y
output(y)
w ← w+1
output(y)
End

Example1: Write an algorithm that computes the square of an integer number using the function
Sqr( ) and use it in the Main( ) function (Using call by reference).

Algorithm Main( )
Begin
input (Num)
Sqr(Num)
output (Num)
End
Algorithm Sqr(&N)
Begin
N ← N*N
End

52 
 
Algorithm Design and Programming Techniques Lecture 6
1st class Asst. Lecture Omar Nowfal

Example2: Write an algorithm that exchanges two integer numbers using the function Swap( )
and use it in the Main( ) function.

Algorithm Main( )
Begin
input (a , b)
output (a , b)
Swap (a , b)
output (a , b)
End
Algorithm Swap (&N1 , &N2)
Begin
temp ← N1
N1 ← N2
N2 ← temp
End

Example3: Write an algorithm that reads the length and width of rectangle and then computes
its area and perimeter using the function AreaPerimeter() and use it in the Main( )
function.
Algorithm Main( )
Begin
input (Len , Wid)
AreaPerimeter (Len , Wid , Area , Perimeter)
output (Area , Perimeter)
End
Algorithm AreaPerimeter (L , W , &A , &P)
Begin
A←L*W
P ← (L + W) *2
End

53 
 
Algorithm Design and Programming Techniques Lecture 6
1st class Asst. Lecture Omar Nowfal

Recursive Function:
A recursive function is a function that calls itself in order to perform a task of computation.
There are two basic components of a recursive solution:
1. Termination Step: Stating the solution when the process comes to an end.
2. Inductive Step: Calling the function itself with a renewed (lesser) parameter value.

Example4: Write an algorithm that computes the factorial of a positive integer number using
the recursive function Factorial( ) and use it in the Main( ) function.

Algorithm Main( )
Begin
input (Num)
output (Factorial (Num))
End
Algorithm Factorial (N) Termination 
Begin Step 
if (N = 0) then Inductive 
return 1 Step 
else
return (N * Factorial (N-1))
End

Calling the function Factorial with argument 4, i.e. Factorial(4):

54 
 
Algorithm Design and Programming Techniques Lecture 6
1st class Asst. Lecture Omar Nowfal

Important Notes:
 Recursive functions must have a termination step and an inductive step.
 Recursive function calls in inductive step must signify a reduction of argument value.
 Be aware of infinite recursion problem.
 Recursion is implicit while iteration is explicit.
 Recursion is slower than iteration.

Example5: Write an algorithm that counts the number of digits of an entered integer number
using the recursive function CountingDigits( ) and use it in the Main( ) function.

Algorithm Main ( )
Begin
input (Num)
output (CountingDigits (Num))
End
Algorithm CountingDigits (N)
Begin

55 
 
Algorithm Design and Programming Techniques Lecture 6
1st class Asst. Lecture Omar Nowfal

if (N/10 = 0) then
return 1
else
return (CountDigits(N/10) + 1)
End

Local and Global Variables:


The major difference between the two is in the scope of the variables. The scope of the variables
designates where the value of the variable can be used.
 Local variables:
o Are defined and used within a function.
o May be used only by the function itself. Other functions have no knowledge of these
variables.
o No problem with variable name duplication in functions.
o If other functions need to use the value of a local variable, the functions must be
coupled through the use of parameters or return values.

 Global variables:
o Can be seen and used by all functions.
o They should be used with caution.
o Be careful not to use the same variable name for a local variable and a global variable.
o The hierarchy of use is to search the local variable list first, the parameter list second,
and the global variable list last.

Consider the following example:

56 
 
Algorithm Design and Programming Techniques Lecture 6
1st class Asst. Lecture Omar Nowfal

57 
 
Algorithm Design and Programming Techniques Lecture 6
1st class Asst. Lecture Omar Nowfal

Example:
define r ← 30
define a ← 0
define b ← 0
Algorithm Main ( )
Begin
define a
Print( )
a←r+1
output (“a = “, a)
Print ( )
End
Algorithm Print ( )
Begin
output (“a= “, a)
End

Homework:
1. Write an algorithm that inputs a time in seconds and then converts the time from seconds
into hours, minutes and seconds using the function ConvertTime( ). Then, print the time
in a formal format (e.g. 12:59:59) using the function DisplayTime( ). Use both functions
in the Main( ) function.

2. Write an algorithm that computes the power of an entered integer number using the
recursive function Power( ) and use it in the Main( ) function.

58 
 

You might also like