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)

Andras Joo said:   1 decade ago
Consider the following exception:

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

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

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.

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();

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.

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;
}

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.

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

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


Post your comments here:

Your comments will be displayed after verification.