5 - Functions - Part3
5 - Functions - Part3
UNIVERSITY
College of Computer Science & IT
Department of CS
Welcome to
CS 221:Fundamentals of Programming
Weeks (5): Functions – Part 3
Chapters 9 and 10 (Zak Textbook)
Chapter 4,5 (Savitch Textbook)
Outline
• Array in functions
• Functions overloading
• Recursion
2
CS221: Fundamentals of Programming
ARRAY IN FUNCTIONS
3
Arrays in Functions
4
Passing array
elements
5
Arrays as Function Arguments
the entire array او نرسلelement by element ممكن نستخدم االراي بالفنكشن باننا نرسلها
• It is not a call-by-reference parameter any change inside the function it will affect
the original value
6
Base Address of an Array and Array in
Computer Memory
• The base address of an array is the address, or memory location
of the first array component
• If list is a one-dimensional array, its base address is the
address of list[0]
• When we pass an array as a parameter, the base address of the
actual array is passed to the formal parameter
اتوقع اول عنصر باالراي اللي يعتبر العنوان حقها يروح للفورمال باراميتر
7
Base Address of an Array and Array in
Computer Memory
يعني من اول
element
بيعرف ال
location
Array Parameter Declaration
ﻟﻤﺎ ﻧﻌﺮف ﻓﻨﻜﺸﻦ وﻧﺮﺳﻞ ﻟﮭﺎ اراي ﻧﺤﻂ اﻗﻮاس وﺗﻜﻮن ﺗﺴﺘﻘﺒﻞ اﻟﺴﺎﯾﺰ ﺑﻌﺪ
9
Function Calls With Arrays
ﻟﻤﺎ ﻧﻨﺎدي اﻟﻔﻨﻜﺸﻦ ﻧﺮﺳﻞ ﻟﮭﺎ اﻻﻛﺸﻞ ﺑﺎراﻣﯿﺘﺮ ﻟﻼراي اﺳﻤﮭﺎ ﺑﺲ ﺑﺪون اﻗﻮاس وﻧﺮﺳﻞ اﻟﺴﺎﯾﺰ ﺑﻌﺪ ﻛﺮﻗﻢ
10
Example: Function Calls With Arrays
11
Function Call Details
fill_up(score, number_of_scores);
اﺳﻢ ﺑﺲ
[] في الفورمال باراميتر نحط
12
Array Formal Parameters
13
Array Argument Details
14
Array Parameter Considerations
fill_up(score, 5);
fill_up(time, 10); ﯾﻌﻨﻲ ﻧﻔﺲ اﻟﻔﻨﻜﺸﻦ اﻗﺪر
ارراي ﻣﺨﺘﻠﻔﯿﻦ٢ ارﺳﻞ ﻟﮭﺎ
ﻛﻞ وﺣﺪة طﻮﻟﮭﺎ ﻣﺨﺘﻠﻒ
15
A Complete Example
#include <iostream> double getAverage(int arr[], int
Using namespace std; size) {
int i;
double getAverage(int arr[], int double avg = 0.0;
size); double sum = 0.0 ;
for (i = 0; i < size; ++i)
int main () { {
/* an int array with 5 elements */ sum += arr[i];
}
int balance[5] = {1000, 2, 3, 17, avg = sum / size;
50}; return avg;
double avg;
}
/* pass pointer to the array as an
argument */
17
Using const With Arrays
وﺑﻌﺪﯾﻦ ﻋﺪﻟﺖ ﻋﻠﻰ اﻟﻘﯿﻢ ﻓﻲ اﻟﻔﻨﻜﺸﻦ ﺑﯿﺼﯿﺮ اﯾﺮورconst ﯾﻌﻨﻲ اذا ﺣﻄﯿﺖ
18
19
20
const Parameters Example
21
Returning An Array
22
Array in Functions Conclusion
• Can you
FUNCTION OVERLOADING
24
Overloading Function Names
25
Overloading Examples
• double ave(double n1, double n2)
{
return ((n1 + n2) / 2);
}
• double ave(double n1, double n2, double n3)
{
return (( n1 + n2 + n3) / 3);
}
– Compiler checks the number and types of arguments
in the function call to decide which function to use
26
Overloading Details
27
Overloading
First Example
Second
Second
First
28
Function Overloading (continued)
29
Define a function Call a function
function formal
return value type
name parameters
int z = max(i, j);
function int max( int num1, int num2)
header {
actual
int result; parameters
if (num1 > num2) function (arguments)
function result = num1; Signature
body else
result = num2;
30
Function Overloading (continued)
• Syntax error:
32
Type Conversion Problem
• Given the previous mpg definition and the following
definition in the same program
int mpg (int goals, int misses)
// returns the Measure of Perfect Goals
{
return (goals – misses);
}
what happens if mpg is called this way now?
cout << mpg(45, 2) << “ miles per gallon”;
– The compiler chooses the function that matches
parameter types, so the Measure of Perfect Goals will be
If I have 2 functions with same name but ( one
calculated double and the other int ), then I call a
function with arguments of type ( int ) the
program will choose the function int
Do not use the same function name for unrelated functions
ﻋﺎدي ﺑﺘﺤﻮﻟﮭﺎ طﺒﯿﻌﻲdouble وھﻲint ﯾﻌﻨﻲ ھﻮ ﻟﻮ ارﺳﻠﺖ ﻟﮭﺎ
int ﺑﯿﺨﺘﺎر اﻟﻔﻨﻜﺸﻦint ﺑﺲ ﻟﻮ ﻛﺎن اﻟﺒﺮوﻗﺮام ﻻزم ﯾﺨﺘﺎر ﺑﯿﻨﮭﻢ وھﻢ ﻧﻔﺲ اﻻﺳﻢ ﺣﺘﻰ ﻟﻮ ان ﺗﺎﯾﺐ اﻟﺒﺎراﻣﯿﺘﺮ ﻣﺨﺘﻠﻒ ﻻﻧﻨﻲ ﻛﺘﺒﺘﮭﺎ
33
CS221: Fundamentals of Programming
35
Recursive Functions
for Tasks
Call to itself
36
Case Study:
Vertical Numbers
• Problem Definition:
37
Case Study:
Vertical Numbers
• Algorithm design:
– Simplest case:
If n is one digit long, write the number
– Typical case:
1) Output all but the last digit vertically
2) Write the last digit
38
Case Study:
Vertical Numbers (cont.)
• The write_vertical algorithm:
if (n < 10) One digit only
{
cout << n << endl;
}
else // n is two or more digits long
{
It will call itself write_vertical(n with the last digit removed);
39
Case Study:
Vertical Numbers (cont.)
• Translating the pseudocode into C++
– n/10 returns n with the last digit removed
• 124/10=12 When divided by 10 the last digit removed
40
Case Study:
Vertical Numbers
(cont.)
41
41
Case Study:
Vertical Numbers (cont.)
41
Tracing a Recursive Call
• write_vertical(123)
if (123 < 10)
{ cout << 123 << endl;
}
write_vertical(123/10);
cout << (123 % 10) << endl;
3
But it will not print unless we complete the first one which is 12
}
43
Tracing write_vertical(12)
• write_vertical(12)
if (12 < 10)
{ cout << 12 << endl;
}
else
// n is more than two digits Calls write_vertical(1)
{
We will send
1 to the
function
write_vertical(12/10);
cout << (12 % 10) << endl;
} 2
44
Tracing write_vertical(1)
• write_vertical(1)
if (1 < 10) Simplest case is now true
{ cout << 1 << endl;
}
Output 1
else
// n is more than two digits
{
write_vertical(1/10);
cout << (1 % 10) << endl;
}
45
A Closer Look at Recursion
46
How Recursion Ends
47
"Infinite" Recursion
ﯾﻌﻨﻲ ﻣﺎﺗﺤﻘﻖ اﻟﺸﺮط اﺑﺪاااا
48
Example: Infinite Recursion
50
Factorial of 4
// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int n = 4;
cout << "Factorial of " << n <<" = " << factorial(n);
return 0;
}
int factorial(int n)
{
if (n > 1)
{
return n*factorial(n-1);
}
else
{return 1;}
}
51
Factorial of 4
52
Factorial of 4
53
Recursion
• What does the following function Do? Suppose
value of begin is 5
void printnum ( int begin )
{
cout<< begin;
if (begin< 9)
// The base case is when begin is greater than 9
printnum ( begin + 1 );
cout<< begin;
}
54
CS221: Fundamentals of Programming
56
Pseudocode for Binary Search
57
Recursive Method for Binary Search
58
Execution of the Method search
(Part 1 of 2)
59
Execution of the Method search
(Part 1 of 2)
60
Checking the search Method
61
Checking the search Method
62
Checking the search Method
63
Checking the search Method
64
Iterative Version of Binary Search
(Part 1 of 2)
65
Iterative Version of Binary Search
(Part 2 of 2)
66
References
• https://www.youtube.com/watch?v=czrbhZjp4JA
• https://www.youtube.com/watch?v=IAMzWp3kS_k
• https://www.youtube.com/watch?v=NvVYd08NUXI
• https://www.programiz.com/cpp-
programming/recursion
67
Any Questions
68