Trying to understand return in C

  • Thread starter Thread starter fisico30
  • Start date Start date
AI Thread Summary
In C programming, the "return" statement is essential for transferring control back to the calling function or the operating system when the main function completes. While void functions do not require an explicit return statement, they implicitly return at the end of their execution. In contrast, functions that return a value must include a return statement to specify the type of value being returned. This allows for multiple return points within a function, enhancing flexibility in control flow. If a function does not include a return statement and is expected to return a value, it can lead to undefined behavior. The discussion also clarifies that while MATLAB does not emphasize return statements, C's requirement serves to ensure that the programmer clearly defines the function's behavior and return types. Additionally, it highlights the importance of variable scope and function parameters, emphasizing that formal parameters must be declared within the function definition.
fisico30
Messages
362
Reaction score
0
Trying to understand "return" in C

Hello Forum,

a function in the C language requires a "return" at the end...is that always true?

what is exactly the purpose of the return? If there is no return, we can just write void or not write anything?

I am familiar with MATLAB where there is no return...why does C requires the specification of return instead?

thanks,
fisico30
 
Technology news on Phys.org


fisico30 said:
Hello Forum,

a function in the C language requires a "return" at the end...is that always true?

what is exactly the purpose of the return? If there is no return, we can just write void or not write anything?

I am familiar with MATLAB where there is no return...why does C requires the specification of return instead?

thanks,
fisico30

Hey fisico30

The purpose of return is to return control of the program back to the routine that called your function. If your function is your entry point (like main), it will return the control back to the Operating System which will terminate your process.

If it is a normal routine it will return control back to the routine that called your procedure.

The reason why C requires this is because of a few main reasons.

The first reason is that you are allowed to have multiple points of returning which means that if the user wants to return back to the code that called the function at any point, then they just put a return statement there and the compiler has to generate the code that the programmer needs to write.

As an example of the above, you could put multiple returns within say if statements depending on the criteria of the if statements and some might return and others might say do something else (like call a function). By allowing the programmer to specify when they want to do things like this, it let's the programmer have the flexibility to do this and the compiler will generate the code that reflects this.

The other reason is that when you are returning an actual object (like when your function is not void) then the compiler needs to make sure you are returning the right object type. The return keyword also includes an object type and the compiler will have to generate code that allocates space either on the stack or using a pointer to return the data specified in the statement.

Basically it boils down to giving the programmer flexibility to tell the compiler specifically what they want done and not let the compiler guess it for them.

Void statements will return at the end of the execution of the function, but again if you have say branching where you want different things to happen (like if a > 2 then run function y else return) then you can explicitly use this for a void function to do what you want.
 


The main() function requires a return

are

return(void), return (0), return the same thing?
 


fisico30 said:
Hello Forum,

a function in the C language requires a "return" at the end...is that always true?
Not true.

If a void function (a function that doesn't return anything) doesn't need a return statement. There is an implicit return statement at the brace that closes the function's definition. Omitting the return statement just before the closing brace is perfectly legal, and is the recommended in some places. Example:
Code:
void update_object (ObjectType * object, double new_quantity) {
   object->quantity = new_quantity;
}
A return statement here is just useless clutter.

Even in functions that do return some value, a return statement at the close brace isn't always needed. If the statement is unreachable, why bother? Example:
Code:
int is_even (int number) {
   if ((number % 2) == 0) {
      return 1;
   }
   else {
      return 0;
   }
}
 


chiro said:
The reason why C requires this is because of a few main reasons.
C does not require this.
ISO/IEC 14882:2003(E) 6.6.3 ¶3 said:
Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function.
A return statement at the end (which is what this thread is about) is not needed in a void function, nor is it needed in a value-returning function if the close brace is unreachable.
 


D H said:
Even in functions that do return some value, a return statement at the close brace isn't always needed. If the statement is unreachable, why bother? Example:
Code:
int is_even (int number) {
   if ((number % 2) == 0) {
      return 1;
   }
   else {
      return 0;
   }
}
I don't think you said exactly what you meant, since there isn't any unreachable code in the example above. Or I might be misunderstanding what you're saying. Maybe you had something like this in mind.

Code:
int is_even (int number) {
   if ((number % 2) == 0) {
      return 1;
   }
   else {
      return 0;
   }
   return 7;
}
The third return statement is unreachable, so it is never executed.
 


One thing not mentioned so far is a peculiarity of English. The verb "to return" is both transitive and intransitive. This means that it can be used with or without a direct object. That might be confusing to some, even native speakers of English, and possibly more so to nonnative speakers

A function can return (go back to the caller), and it can return (send back) a value.
 


So, in general a function comprises, between its curly brackets, some instructions.

A function can have its own input arguments, in between its parenthesis after its name...

But a function can work with arguments that are not necessarily defined and declared inside the round brackets: for instance, in the example above,

int is_even (int number)

the name is is_even, and the input is the variable number...we could have declared the variable number before the function and used it inside the function is_even, correct?

the return 0 and return 7 simply associate either value to the function because the output of the function is an integer...

Many functions simply execute some instructions...I am familiar with Matlab, where return is not used and a function simply provides a set of instructions...

thanks,
fisico30
 


Matlab most certainly does have a return statement, and its behavior is exactly the same as C's.
 
  • #10


fisico30 said:
So, in general a function comprises, between its curly brackets, some instructions.
Sort of. The function definition consists of the type returned by the function, the function name, the list of formal parameters, and the body of the function (between the braces).

You can also have a function declaration (or prototype), in which you tell the compiler the return type, the function name, and the number and types of the formal parameters.
fisico30 said:
A function can have its own input arguments, in between its parenthesis after its name...
Yes.
fisico30 said:
But a function can work with arguments that are not necessarily defined and declared inside the round brackets: for instance, in the example above,

int is_even (int number)
No. The formal parameters must be declared. In your example, number is the formal parameter, and its type is int.

I might not be understanding your question. Are you talking about calling the function?
fisico30 said:
the name is is_even, and the input is the variable number...we could have declared the variable number before the function and used it inside the function is_even, correct?
No. If you declare number as an int inside the main function, the scope of that variable is just the main function. That means that the name number is not known outside main. If you declare number at the global level, you still have to declare the name of the formal parameter of your is_even function.

If you have declared number as a global variable, and you declare it as a formal parameter of the is_even function, then what you have are two variables with the same name.
fisico30 said:
the return 0 and return 7 simply associate either value to the function because the output of the function is an integer...
That's an odd way to put it. The returned value is actually sent back to the caller.

Code:
int fun(int); // function prototype

int main(void)
{
   int input = 3;
   int result;
   result = fun(input);
   return 0;
}

// function definition
int fun(int value)
{
   return 7;
}

In the code above, the value 3 is passed to fun, and fun immediately returns 7, which is then stored in result.
fisico30 said:
Many functions simply execute some instructions...
Fortran distinguishes between routines that return a value (FUNCTION) and routines that cause something to happen (SUBROUTINE).

In C and C++ functions either return some value or are void functions.
fisico30 said:
I am familiar with Matlab, where return is not used and a function simply provides a set of instructions...

thanks,
fisico30
 
Back
Top