C Programming - Functions - Discussion
Discussion Forum : Functions - Yes / No Questions (Q.No. 3)
3.
If a function contains two return statements successively, the compiler will generate warnings. Yes/No ?
Answer: Option
Explanation:
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
Discussion:
13 comments Page 2 of 2.
Vijay said:
1 decade ago
#include<stdio.h>
int mul(int, int); /* Function prototype declaration */
int main()
{
int a = 4, b = 3, c;
c = add(a, b);
printf("c = %d\n", c);
return 0;
}
int mul(int a, int b) /* Function definition*/
{
if(a>b)
return (a * b);
return (a - b);
}
I think in this case they can come one after the other.
int mul(int, int); /* Function prototype declaration */
int main()
{
int a = 4, b = 3, c;
c = add(a, b);
printf("c = %d\n", c);
return 0;
}
int mul(int a, int b) /* Function definition*/
{
if(a>b)
return (a * b);
return (a - b);
}
I think in this case they can come one after the other.
Niziak said:
1 decade ago
Warning is not error, so I didn't see problems with two returns :)
Andras Joo said:
1 decade ago
Consider the following exception:
if (condition) goto lab1;
goto lab2;
//...
lab1: return 1;
lab2: return 0;
if (condition) goto lab1;
goto lab2;
//...
lab1: return 1;
lab2: return 0;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers