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 1 of 2.

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.
(1)

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

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

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.

Pushpa said:   1 decade ago
Warning is not come in gcc compilation.

Abhishek said:   1 decade ago
In a recursive function, there may have more than one return statement. But then its not generating any warning. Then how ans can be yes? consider this example:

#include<stdio.h>
int large(int,int);
main()
{
int a=3;
int b=4;
int c;
c=large(a,b);
printf("%d",c);
return 0;
}
int large(int x,int y)
{
if(x>y)
return(x);
else
return(y);
}

/*it will return y value successfully. It will not give any warning.*/

Jagadeesh said:   1 decade ago
When ever in a function return statement is executed imedietly, the function that was called will take control over the program so it is not matter of no of return statements but the first one only gets executed.

I aslo tested in GNU compiler and also in Dev C++ both are not generating any warning.

John said:   1 decade ago
I get no warnings at all when compiling a program containing this:

int USART_Transmit( char data )
{
while ( !( UCSRA & (1<<UDRE)) );/* Wait for empty transmit buffer */
UDR = data;/* Put data into buffer, sends the data */
return 0;
return 0;
}

Manche Shekhar said:   1 decade ago
For this program the output is:

In function `main':
undefined reference to `add'

Because here we declare mul( ) function but we are passing values to add.

Ajeet Kumar said:   1 decade ago
int mera(int,int);
void main()
{
int a,b,c;
printf("Enter the two value which u want");
scanf("%d%d",&a,&b);
c=mera(a,b);
printf("The final value is:=%d",c);
getch();
}

int mera(int a,int b)
{
if(a>b)
{
return (a-b);
}
else
{
return (a*b);
}
}
getch();


Post your comments here:

Your comments will be displayed after verification.