Functions(C Programming ) Questions and Answers
Explanation:-
Answer: Option B. -> No
No, In a function two return statements can occur but not successively.
Example:
#include<stdio.h>
int mul(int, int); /* Function prototype */
int main()
{
int a = 0, b = 3, c;
c = mul(a, b);
printf("c = %d\n", c);
return 0;
}
/* Two return statements in the mul() function */
int mul(int a, int b)
{
if(a == 0 || b == 0)
{
return 0;
}
else
{
return (a * b);
}
}
Output:
c = 0
Explanation:-
Answer: Option A. -> YesYes, too many recursive calls may result into stack overflow. because when a function is
called its return address is stored in stack.
After sometime the stack memory will be filled completely. Hence stack overflow error will occur.
Explanation:-
Answer: Option A. -> YesWhen a recursive call is made, the function/process clones itself and then process that funtion.
This leads to time and space constrains.
In a loop, there is no recursive call involved that saves a lot of time and space too.
Explanation:-
Answer: Option A. -> Yes
Yes, It will return the value 20*20 = 400
Example:
#include<stdio.h>
int f1(int, int); /* Function prototype */
int f2(int); /* Function prototype */
int main()
{
int a = 2, b = 3, c;
c = f1(a, b);
printf("c = %d\n", c);
return 0;
}
int f1(int a, int b)
{
return ( f2(20) );
}
int f2(int a)
{
return (a * a);
}
Output:
c = 400
Explanation:-
Answer: Option A. -> Yes
Yes. If a function contains two return statements successively, the compiler
will GENERATE "Unreachable code" warnings.
Example:
#include<stdio.h>
int mul(int, int); /* Function prototype */
int main()
{
int a = 4, b = 3, c;
c = mul(a, b);
printf("c = %d\n", c);
return 0;
}
int mul(int a, int b)
{
return (a * b);
return (a - b); /* Warning: Unreachable code */
}
Output:
c = 12
Explanation:-
Answer: Option B. -> No
A function can return floating point value.
Example:
#include<stdio.h>
float sub(float, float); /* Function prototype */
int main()
{
float a = 4.5, b = 3.2, c;
c = sub(a, b);
printf("c = %f\n", c);
return 0;
}
float sub(float a, float b)
{
return (a - b);
}
Output:
c = 1.300000