How return works and what is it used for? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How return works and what is it used for?

I just finished Introduction to C and the fact on how return works still scratches my head. Like when you define a function and put return on the code that will be executed and it does nothing unless you call the function. I believe the function still works normally even without the “return”. How return works and what is it even used for😭😭😭

28th Jun 2024, 9:21 AM
Loo Xin Zhe
Loo Xin Zhe - avatar
4 Answers
+ 4
Loo Xin Zhe , Return statement is used to end the execution of the corresponding function and as a result it returns the value... For function call return statement is needed...
28th Jun 2024, 9:31 AM
Riya
Riya - avatar
+ 3
Return is really a important keyword in C Function can be run without return when their data type is void Return doest print any thing but it help to assign that value for other important purpose or also to run that specific function without outputting something sometimes we dont want a specific function to print something but we want to executed it at that time return arrives and also only void functions can perform well without return value, other required return value.
28th Jun 2024, 11:36 AM
Alhaaz
Alhaaz - avatar
+ 3
There are three ways to use a function in C. Two of the ways do not require a return statement. One of them requires it. Return not required: 1. The function is a procedure that performs an action only: turn on a light, or output "Hello", or activate a motor, etc.... Return not required: 2. Arguments are passed into the function as pointers, and the function is a procedure that only modifies the arguments in their original memory locations: change a string within its original memory location, or sort an array in the original location, or calculate and update multiple arguments with new values, etc... Return required: 3. The code that calls the function needs the function's final result in an assignment statement, or a calculation, or an expression of any kind. The return statement exits the function and sends the final result back to the caller. e.g., y = cos(x); // y = the return value. These are the base concepts of function calling. You can have functions that do a mix of any or all three of the above.
28th Jun 2024, 3:40 PM
Brian
Brian - avatar
0
Wow i think I’m understanding it a bit more😅
29th Jun 2024, 1:25 PM
Loo Xin Zhe
Loo Xin Zhe - avatar