C Programming - Functions - Discussion

3. 

If a function contains two return statements successively, the compiler will generate warnings. Yes/No ?

[A]. Yes
[B]. No

Answer: Option A

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


Andras Joo said: (Oct 1, 2010)  
Consider the following exception:

if (condition) goto lab1;
goto lab2;
//...
lab1: return 1;
lab2: return 0;

Niziak said: (Jan 1, 2011)  
Warning is not error, so I didn't see problems with two returns :)

Vijay said: (Jan 23, 2011)  
#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.

Ajeet Kumar said: (Jun 2, 2011)  
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();

Manche Shekhar said: (Mar 26, 2012)  
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.

John said: (Jun 8, 2012)  
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;
}

Jagadeesh said: (Jun 10, 2012)  
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.

Abhishek said: (Jul 2, 2013)  
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.*/

Pushpa said: (Feb 11, 2014)  
Warning is not come in gcc compilation.

Suresh said: (Oct 6, 2014)  
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: (Nov 18, 2015)  
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: (Oct 6, 2016)  
Some compilers don't make warnings. The code is simply unreachable. What is the reason?

Kalidas Tate said: (Dec 30, 2016)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.