C Programming - Functions - Discussion
Discussion Forum : Functions - True / False Questions (Q.No. 6)
6.
A function may have any number of return statements each returning different values.
Answer: Option
Explanation:
True, A function may have any number of return statements each returning different values and each return statements will not occur successively.
Discussion:
8 comments Page 1 of 1.
Raj said:
8 years ago
Function addmul()has been called twice so it seems like fun returning two val, the only 1st return will get executes every time. Just make comment to 2nd return you will get same answer.
Aparna Singh said:
8 years ago
When the statement return(kk) is executed, the value of "kk" is returned and the control goes back to the calling function i.e to the statement k=addmult(i,j);.
So, the value of another variable "ll" is not returned.
True, A function may have any number of return statements each returning different values and each return statements will not occur successively. Here return(kk) and return(ll) are occuring successively.
So, the value of another variable "ll" is not returned.
True, A function may have any number of return statements each returning different values and each return statements will not occur successively. Here return(kk) and return(ll) are occuring successively.
Rifat said:
9 years ago
Chirram Kumar. I have same doubt somebody please explain this why the 2 values are not returned in above program.
Erik said:
10 years ago
I totally agree with the answer to this question but I feel as if it could be worded correctly. It's a little obtuse, and could be easily misconstrued to something like "A function can return multiple values".
Unless this question is intended to be a "trick" question I feel as if the wording could be reworked.
Unless this question is intended to be a "trick" question I feel as if the wording could be reworked.
Chirram Kumar said:
1 decade ago
#include<stdio.h>
int addmult(int ii, int jj)
{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return (kk);
return(ll);
}
int main()
{
int i=3, j=4, k, l;
k = addmult(i, j);
l = addmult(i, j);
printf("%d %d\n", k, l);
return 0;
}
From the explanation of the question 6.
A function may have
Any number of return statements each returning different values and each return statements will not occur successively.
In the above program two return statements are occur successively
But here the output of the program is 7,7
int addmult(int ii, int jj)
{
int kk, ll;
kk = ii + jj;
ll = ii * jj;
return (kk);
return(ll);
}
int main()
{
int i=3, j=4, k, l;
k = addmult(i, j);
l = addmult(i, j);
printf("%d %d\n", k, l);
return 0;
}
From the explanation of the question 6.
A function may have
Any number of return statements each returning different values and each return statements will not occur successively.
In the above program two return statements are occur successively
But here the output of the program is 7,7
Sonia said:
1 decade ago
Thanks Sundar.
(1)
Sundar said:
1 decade ago
int myfunction(int x)
{
if (x == 1) return 100;
if (x == 2) return 200;
if (x == 3) return 300;
return 500;
}
The above function has 4 return statements, and each return statement will return different values.
If a return statement executed, rest of the code of that function will be ignored, control will be transferred to calling function.
Hope this will help you. Have a nice day!
{
if (x == 1) return 100;
if (x == 2) return 200;
if (x == 3) return 300;
return 500;
}
The above function has 4 return statements, and each return statement will return different values.
If a return statement executed, rest of the code of that function will be ignored, control will be transferred to calling function.
Hope this will help you. Have a nice day!
(1)
Kundan said:
1 decade ago
How is it working? Any one explain with suitable example.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers