Home > other >  Why do we need at the end of the C function using 'return 0'?
Why do we need at the end of the C function using 'return 0'?

Time:11-27


I don't understand why we need at the end of function use return 0; , I don't know why we need it in main () at the end of (to the end? , but I really don't understand why we need it at the end of the normal function,








CodePudding user response:


If you would return the type declaration for void

CodePudding user response:


You do not need to return zero in each function, for example:
Void my_function (int * p_data)
{
* p_data=https://bbs.csdn.net/topics/5;
}

Int main (void)
{
The int data=https://bbs.csdn.net/topics/0;
My_function (& amp; The data);
Data: printf (" % d ", Data);

return 0;
}

This will work well,

CodePudding user response:


If in your code, you said your () function returns a value, but you did not actually returns a:
Int my_function (int x)
{
//bad practice!
}

. This is wrong is bad, you said you want to return a value (in this case is' int '), but you don't,
Also, if there are multiple paths through your functions:
Int my_function (int x)
{
If (x==3)
Return 42;//good

//bad!
}

You also make a statement of intent return a value, but by your function some path does not do so, it is wrong is bad; You have violated the caller's commitment to a function,
Through the function of all paths should be at the end of the return statement:
Int my_function (int x)
{
If (x==3)
Return 42;//good

return 1;//good
}

If you don't want to return anything, then a statement function void:
Void my_function (int x)
{
//good
}

UPDATE: the above, you may notice that I will be "wrong" replaced by "bad practices," this is because the word "error" in the C standard is very special, and forget the return statement incompliance definition (on the contrary, it is considered to be "undefined behavior", and your compiler might not even complain), however, you as a person should think it is a mistake in your code, and avoid the function of all forget return statement, exit path

CodePudding user response:


If a function declaration for the outside return void type, then it must have a return statement, the only exception is the main function, the function from the C99 can omit return statements (omit, behavior and before the closing of the main} return 0; The statement is the same),
Considering the error function test: if you call the test as a parameter to 0
Int test (int a)
{
If (a==0)
The return of 123;
}

Int main ()
{
int x;
X=test (0);//x equals 123
X=test (1);//x equals???????
}

, the function will give you back to 123, but if you call outside of 0? What is your desired outcome? For the function in the C, there is no "the default return value", if the return type is not void (one exception is the main function), the return value must be,

CodePudding user response:

If we can understand the main function is now don't need to return 0; But if a custom function need the return value is still need to return
  • Related