In: Computer Science
Can someone please explain the following program to me? I have ran it, but do not understand how the answers are being derived.
#include<iostream>
using namespace std;
int num1 = 1;
int quiz(int num)
{
static int n1 = 10;
int n2 = 20;
n1--;
n2++;
num1++;
if (num == 1)
return num1;
else if (num < 3)
return n1;
else
return n2;
} // quiz
main()
{
cout << quiz(num1) << endl;
cout << quiz(num1) << endl;
cout << quiz(1) << endl;
cout << quiz(num1) << endl;
} // main
In this code there is function i.e quiz function. This function is called 4 times in the main method. Let us see one by one what happens on every function call.
This Table shows how the program gets executed:
Function call no. |
Num |
N1 |
N2 |
Num1 |
N1-- |
N2++ |
Num1++ |
If(Num==1) Return num1 |
Elseif(Num<3) Return n1 |
Else Return n2 |
Output |
1.quiz(num1) |
1 |
10 |
20 |
1 |
9 |
21 |
2 |
1==1 True return 2 |
2 |
||
2.quiz(num1) |
2 |
9 |
20 |
2 |
8 |
21 |
3 |
False |
2<3 True Return 8 |
8 |
|
3.quiz(1) |
3 |
8 |
20 |
1 |
7 |
21 |
4 |
1==1 True return 4 |
4 |
||
4.quiz(num1) |
4 |
7 |
20 |
4 |
6 |
21 |
5 |
False |
False |
return 21 |
21 |
Explanation for the table is given below:
1st function call quiz(num):
Here num1 =1 ,So this value is given to the function.
Now inside the function
n1=10 (this is a static variable so it creates the copy i.e It has one memory address and if the value of n1 is changed then during next function call it will use that changed value )
n2=20(This is not static , so for every function call it will remain same)
Now ,the value of n1 =9 (As it is decremented by 1)
Value of n2=21(As it is incremented by 1)
num1=2 (As it is incremented by 1 )
Condition check is If(num==1) (Value of num1 will be the one which is passed through the function i.e 1)
So ,(1==1) which is True
Now it will return the value of num1 =2 (The incremented value)
For the 1st function call we get value as 2.
2nd function call quiz(num):
Here num1 = 2 ,So this value is given to function.(We got num1 =2 after incrementing it in the 1st function call).
Now inside the function
n1=9 (As it is static so it stores the previous value )
n2=20 (As it is not static so it will start from 20 , not from 21)
Now the value of n1=8(decremented by 1)
Value of n2=21(Incremented by 1)
num1=3(Incremented by 1)
Condition check is If(num==1) (Value of num1 will be the one which is passed through the function i.e 2)
So,The if condition is not True
It will check the else if (num<3)
So, (2<3) which is True.
Now it will return the value of n1=8 (The one which we got after decrementing)
For the 2nd function call we get value as 8.
3rd function call quiz(1):
Here 1 is given to function.
Now inside the function
n1 = 8(As it is static so it stores the previous value )
n2=20(As it is not static so it will start from 20 , not from 21)
num1=3(We got num1 =3 after incrementing it in the 2nd function call).
Now the value of n1=7(decremented by 1)
Value of n2=21(Incremented by 1)
num1=4(Incremented by 1)
Condition check is If(num==1) (Value of num will be the one which is passed through the function i.e 1)
So ,(1==1) which is True .
Now it will return the value of num1 =4 (The incremented value)
For the 3rd function call we get value as 4.
4th function call quiz(num1):
Here num1 = 4 ,So this value is given to function.(We got num1 =4 after incrementing it in the 3rd function call)
Now inside the function
n1 = 7(As it is static so it stores the previous value )
n2=20(As it is not static so it will start from 20 , not from 21)
num1=4(We got num1 =4 after incrementing it in the 3rd function call).
Now the value of n1=6(decremented by 1)
Value of n2=21(Incremented by 1)
num1=5(Incremented by 1)
Condition check is If(num==1) (Value of num1 will be the one which is passed through the function i.e 4)
So,The if condition is not True
It will check the else if (num<3)
So, else if condition is not True
Now, it will go to the else part and the return the value n2=21
For the 4th function call we get value as 21.
Final Output:
2
8
4
21