Trying to understand return in C

  • Thread starter fisico30
  • Start date
In summary, in C, a return statement at the end of a function indicates that the function is done and the program should return to the calling routine. If there is no return, the function just ends.
  • #1
fisico30
374
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
  • #2


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.
 
  • #3


The main() function requires a return

are

return(void), return (0), return the same thing?
 
  • #4


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;
   }
}
 
  • #5


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.
 
  • #6


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.
 
  • #7


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.
 
  • #8


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
 
  • #9


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
 

FAQ: Trying to understand return in C

1. What is a "return" in C and why is it important in programming?

A "return" in C is a statement that is used to exit a function and return a value to the calling program. It is important in programming because it allows the program to pass information back to the main program, which can then use that information for further processing or decision making.

2. How do you use a "return" statement in C?

To use a "return" statement in C, you first need to declare the function that will contain the "return" statement. Within the function, you can use the "return" keyword followed by the value or variable that you want to return. For example:
return 5;
This statement will return the value 5 to the calling program.

3. Can you have multiple "return" statements in a function?

Yes, you can have multiple "return" statements in a function. However, only one "return" statement will be executed in a function. This means that once a "return" statement is encountered, the function will exit and return the specified value to the calling program.

4. What happens if a "return" statement is not included in a function?

If a "return" statement is not included in a function, the function will still execute but it will not return any value to the calling program. This can cause errors or unexpected results in the main program if the function was intended to pass back information.

5. Is it necessary to use a "return" statement in every function?

No, it is not necessary to use a "return" statement in every function. There are some functions that do not need to return a value and can simply perform a task or modify a variable. In these cases, the function can be declared as a "void" function, meaning it does not return anything.

Similar threads

Replies
2
Views
1K
Replies
17
Views
3K
Replies
17
Views
1K
Replies
3
Views
2K
Replies
2
Views
969
Replies
28
Views
3K
Replies
36
Views
2K
Replies
4
Views
887
Back
Top