Search code examples
creturnreturn-value

Why main ignores the returning value of the user defined function


We all know that return statement ends the execution of the called function and returns some value to the calling function but in the case given below I didn't accept the returning value of the user-defined function via any variable or as an argument in another function like as in printf then why main printed Hello , according to my thinking main's execution should end after printing 5 as 0 is returned to it, I have searched it on various platforms but they just stated that main ignores the value returned (but why and how ?)

#include <stdio.h>

//Compiler version gcc  6.3.0

int main()
{ 
  printf("%d\n",6);
  Array();
  printf("Hello\n");
  return 0;
}

int Array()
{
  int c=5;
  printf("%d\n",c);
  return 0;
}

Solution

  • The line return 0; only ends the function it belongs to. In Array(), it will end the execution of Array() function, and nothing else. The fact that main ignores the return value, has nothing to do with that. But to answer the "why and how" it ignores it - You didn't tell main where you want to store the return value of Array(), so it won't store it anywhere.
    If you said int a = Array();, then main would know to store it in variable a. But if you just call Array();, then main will not store the return value anywhere, therefore you could say it ignored it.