You are on page 1of 2

Function Exercise

1. A program in C language is given as follows.


#include <stdio.h> //Line 1
int m_power_n(int,int); //Line 2
int i, result=1; //Line 3
void main() //Line 4
{ //Line 5
int a, b; //Line 6
//Line 7
scanf(“%d %d”, &a, &b); //Line 8
result = m_power_n(a,b); //Line 9
printf(“\n%d to the power of %d is %d.”, a,b, result); //Line 10
} //Line 11
//Line 12
int m_power_n(int r, int s) //Line 13
{ //Line 14
int result =1; //Line 15
//Line 16
for( i=0; i < s; i++) //Line 17
result * = r; //Line 18
return result; //Line 19
} //Line 20

(a) State the scope of the following identifiers.


(i) The identifier result in Line 3
[1 mark]
(ii) The identifier r in Line 13
[1 mark]
(iii) The identifier result in Line 15
[1 mark]
(b) What is the output of the above program if inputs a and b are 4 and 3
respectively?
[2 marks]

Function Exercise
1. A program in C language is given as follows.
#include <stdio.h> //Line 1
int m_power_n(int,int); //Line 2
int i, result=1; //Line 3
void main() //Line 4
{ //Line 5
int a, b; //Line 6
//Line 7
scanf(“%d %d”, &a, &b); //Line 8
result = m_power_n(a,b); //Line 9
printf(“\n%d to the power of %d is %d.”, a,b, result); //Line 10
} //Line 11
//Line 12
int m_power_n(int r, int s) //Line 13
{ //Line 14
int result =1; //Line 15
//Line 16
for( i=0; i < s; i++) //Line 17
result * = r; //Line 18
return result; //Line 19
} //Line 20

(a) State the scope of the following identifiers.


(i) The identifier result in Line 3
[1 mark]
(ii) The identifier r in Line 13
[1 mark]
(iii) The identifier result in Line 15
[1 mark]
(b) What is the output of the above program if inputs a and b are 4 and 3
respectively?
[2 marks]

You might also like