Beruflich Dokumente
Kultur Dokumente
Function (Modules):
Function is a small set of instructions that perform specific task and return a value.
Functions are used as parts of instructions in a solution.
Because functions are basic tasks that are used repeatedly in the problem solving process,
by using them a programmer can shorten the problem-solving time and improve the
readability of the solution.
The value of the result of the function is returned in the name of the function.
Libraries of functions can be created and added to many languages.
Functions can be:
o Built-in a programming language, created by the language designer.
o User-defined functions created by the programmer.
Parameters:
are local variables that are passed or sent from one function to another.
They are placed after the function name and within parentheses, for example: Read(A, B,
C).
Many parameters may be passed to a function, while only one value may be returned
from it.
Functions normally do not alter the parameters.
Not all functions need parameters or return value.
A parameter can be a constant, a variable, or an expression.
Function Definition:
43
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
Example1:
Algorithm CalcAverge(x , y)
Begin
return ((x + y) / 2)
End
Function Call:
The function can be called (or invoked, or executed) many times in the program or
other functions.
The calling function is the function that processes another function.
The called function is the function being processed.
Form of calling:
Function_name(parameter list)
Example2:
Print( )
Fact ← Factorial (n)
output (CalcAverage (90 , 70)
Executing the call action causes the function to execute; that is, control is transferred to the
function, the actions in the function definition are executed, and then control returns to the action
following the function call.
44
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
Example3: Write an algorithm to calculate the average of between two numbers using function
Averge( ).
Algorithm main( )
Begin
Input (A, B)
Av ← Averge (A, B)
output (Av)
End
Algorithm Starsline( )
Begin
for i ← 1 to 25 do
output (“*”)
output (NewLine)
End
Example5: Write an algorithm that adds two integer numbers using the function Add( ) and use
it in the Main( ) function.
Algorithm Main( )
Begin
input (A, B)
Sum ← Add (A, B)
output (sum)
End
45
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
1) Passing by Value:
Example6: Write an algorithm that computes the square of an integer number using the function
Sqr( ) and use it in the Main( ) function.
Algorithm Main ( )
Begin
input (Num)
output (Sqr(Num))
End
Example7: Write an algorithm that finds the maximum of two entered numbers using the
function Max( ) and use it in the Main( ) function.
Algorithm Main ( )
Begin
input (A , B)
M ← Max (A , B)
output (M)
End
46
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
Example8: Write an algorithm that computes the factorial of an integer number using the
function Factorial( ) and use it in the Main( ) function.
Algorithm Main( )
Begin
input (X)
output (Factorial(X))
End
Example9: Write an algorithm that computes the power of an integer number using the function
Power( ) and use it in the Main( ) function.
Algorithm Main ( )
Begin
input (Num, Pow)
output (“The Power” , Pow , “ of the number “ , Num , “ = “ , Power (Num , Pow))
End
47
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
Algorithm Power (N , P)
Begin
R←1
for i ← 1 to P do
R←R*N
return (R)
End
Example10: Write an algorithm that computes the shaded area in the figure below using the
function Area() and use it in the Main( ) function.
Algorithm Main ( )
Begin
LE ← 10
WE ← 20
LI ← 7
WI ← 15
output (“Shaded area = “ , Area (LE , WE) – Area (LI , WI))
End
Algorithm Area (L , W)
Begin
return (L * W)
End
48
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
Example11: Write an algorithm that tests an entered character if it is a small letter or not using
the function Issmall( ) and use it in the Main( ) function.
Algorithm Main ( )
Begin
input (Ch)
if (Issnall(Ch)) then
output (“Small Latter”)
else
output (“Not Small Latter”)
End
Example12: Write an algorithm that finds the max value in an integer array a[10] using the
function MaxArr( ) and use it in the Main( ) function.
Algorithm Main( )
Begin
for i ← 0 to 9 do
input (A[i])
output (“The Max. value of array A is “, MaxArr (a))
End
return (Max)
End
Example13: Write algorithms that read, sort and print an integer array a[10] using three
functions Setarray( ), Sortarray( ), and Putarray( ) respectively and use them in the
Main( ) function.
Algorithm Main ( )
Begin
Setarray (A)
Sortarray (A)
Putarray (A)
End
50
Algorithm Design and Programming Techniques Lecture 5
1st class Asst. Lecture Omar Nowfal
Homework:
1. Write an algorithm that determines whether an integer number is positive, negative or
zero using the function Test( ).
2. Write an algorithm that computes the shaded area in the figure below using the function
CircleArea( ). Use it in the Main( ) function. The radius values should be entered by the
user.
3. Write an algorithm that converts a positive integer number into binary using the function
Binary( ) and use it in the Main( ) function.
4. An integer number is said to be a prime if it is divisible only by 1 and itself. Write an
algorithm that inputs an integer number and determines whether the number is a prime or
not using the function Isprime( ) and use it in the Main( ) function.
5. Write an algorithm to test if an entered character is a numeric digit or not using the
function Isndigit( ) and use it in the Main( ) function.
6. Write an algorithm that computes the number of decimal digits in an entered positive
integer number using the function Nodec() and use it in the Main( ) function.
7. Write an algorithm that converts a small letter into capital letter using the function
ToCapital( ) and use it in the Main( ) function.
51