Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the return value from a function without a return statement?

#include<stdio.h>
int sel=5,out=10;

int findout(){
    if(sel==5)
        return out*=2;
}

int main(){
    int ret1,ret2=-1;
    ret1=findout();
    printf("before %d %d %d",sel,out,ret1);
    sel=8;out=7;
    ret2=findout();
    printf("\nafter %d %d %d",sel,out,ret2);
}

Output:

before 5 20 20

after 8 7 8

EDIT: My compiler doesn't show any warnings.Here you can see it. Its on codeblocks GNU GCC compiler on Ubuntu OS

g++   -c Untitled1.cpp -o Untitled1.o
g++  -o Untitled1 Untitled1.o   
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Here in second case,when I did not return any value (for sel=8 and out =7) how come the value of ret2 is 8?

like image 213
Himani Virmani Avatar asked Nov 28 '22 13:11

Himani Virmani


1 Answers

When sel is not 5 your function findout() does not go through a return statement.

That is illegal C; and your compiler should warn you about it.

Do not ignore your compiler warnings.

like image 64
pmg Avatar answered Dec 08 '22 01:12

pmg