Trying to understand return in C

  • Thread starter Thread starter fisico30
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers on the use of the "return" statement in C programming, specifically its necessity and purpose within functions. Participants explore the differences between C and other programming languages, such as MATLAB, regarding function returns and the implications of using or omitting return statements.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • Some participants assert that a return statement is necessary for functions in C, particularly for returning control to the calling routine.
  • Others argue that void functions do not require a return statement, as there is an implicit return at the end of the function.
  • It is noted that a return statement is not needed in value-returning functions if the closing brace is unreachable.
  • One participant highlights the flexibility provided by multiple return points within a function, allowing for different execution paths based on conditions.
  • There is a discussion about the implications of using return statements in functions, including the potential for undefined behavior if a value-returning function does not return a value.
  • Some participants clarify the distinction between a function returning control and returning a value, referencing the dual meanings of "to return" in English.
  • There is a debate about the nature of function parameters and scope, with some participants clarifying that formal parameters must be declared within the function definition.
  • One participant claims that MATLAB does not use a return statement, while another counters that MATLAB does have a return statement with behavior similar to C's.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of return statements in C functions, with some asserting their importance while others contend that they are not always required. The discussion remains unresolved regarding the specifics of return behavior in various contexts.

Contextual Notes

There are nuances regarding the scope of variables and the definition of formal parameters that are not fully resolved. Additionally, the implications of using return statements in different programming contexts are still being debated.

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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
12K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K