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 ?
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.

Suresh said:   1 decade ago
1 #include<stdio.h>
2 int print()
3 {
4 int b = 7,a = 2;
5 printf("This is my machine\n");
6 return (a+b);
7 return (a-b);
8 }
9
10 int main()
11 {
12
13 int x = print();
14 x++;
15 printf("returned value is : %d\n ",x);
16 return 0;
17 }

This program, in GCC does not give any warnings.

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 */
}

Shubham Tripathi said:   9 years ago
Some compilers don't make warnings. The code is simply unreachable. What is the reason?


Post your comments here:

Your comments will be displayed after verification.