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.
Luki said:
10 years ago
Visual Studio also doesn't generate warning for this code:
int mul(int a, int b)
{
return (a * b);
return (a - b); /* Warning: Unreachable code */
}
int mul(int a, int b)
{
return (a * b);
return (a - b); /* Warning: Unreachable code */
}
Shubham Tripathi said:
9 years ago
Some compilers don't make warnings. The code is simply unreachable. What is the reason?
Kalidas Tate said:
9 years ago
Same here,
I have compiled same code which has two successive return statements with GCC version 4.8.3 20140627.
I didn't get any warning.
I have compiled same code which has two successive return statements with GCC version 4.8.3 20140627.
I didn't get any warning.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers