Instant Search

Why we use Return in function ? Return Statement purpose

So i was reading about function in more deep and i found that there are not much explanation of “Return of function” with stack. sO I thought to write about it.

So question is “What is return and why we need it ? What return statement does?”

So let’s understand by some examples we are going to take 2 examples one is without return statement and another is with return statement :

Before understanding this example keep two things in mind :

foo=function()  so foo is just a reference of function() and foo always store that value which return by the function whom its referencing.

second foo always store the value of that function whom its referencing and we can use that result later in program anywhere so its a advantage.

 

A function always return something , yes now you will say soemtimes i don’t use return statement then still function return , My answer is yes ! yes ! yes !

Function always return even when you don’t return in function then function return “None” which is default value of function return.

Without return statement :

As you can see its gave the error because :

Because as i said foo=function()  , foo always store that value which function() return so here our function is jk() and here our foo is “n” so “n” will store that value which jk() will return and we have not returned anything so default value which is None have returned and now “n” store the value of returned which is “None” and that why its giving error that “NoneType” object is not callable.

 

Now take a look of same program with return statement :

As you can see its main function returning the nested function “nm” and as i said referencer always store that value so n will store calling of “nm” which is nested funtion. Now when we will use n() then n() means calling the nested function nm()

Now take a simple look of example how return effect closure values :

 

here we are returning 0 and f is storing “0” so now when we will call f(12) then it will give error because f is 0 which is an integer that’s why we can’t use it as a function.

When a def is met it creates the function , gives it  a name and move on , then when its called it create a new stack frame (a bit of memory to store each of local variable) and remember where it was when it called it.

then , it execute the code inside the function , finishes , deletes the stack frame (pops it off the stack) and return to where it was and continues (this is control flow)
The print() function writes, i.e., “prints”, a string in the console. The return statement causes your function to exit and hand back a value to its caller. The point of functions in general is to take in inputs and return something.

 

 

 

a function can take some input (usually one or more variables or values) and return some output (usually a single value).


Not every function needs to return a value. Some just don’t. If all you want is to print the number, then you don’t really need to return it. So in the following example you won’t notice any difference:

 

But consider another scenario, where you’d want to use the returned value (the output) inside another calculation or expression:

See, now there’s trouble. To come back to the functions you asked about, you can, for instance, use the smallest_number in another expression, and you can only do that because of the return keyword:

See, now there’s trouble. To come back to the functions you asked about, you can, for instance, use the smallest_number in another expression, and you can only do that because of the returnkeyword:

 

Here is the example how funtion store in stack :

 

 

 

 

 

 

 

 

Before understanding of this program i recommend watch this video :

So in that video there are four procedure and i have done that program live in python so here we have four function which are calling each other now watch the video first and then understand in live example by my code :

Bonus :

 

Let me tell you a short story which will help you to understand the return statement and its work more. Suppose a guy from city1 doing some work and he need a programmer so he searched and found a programmer from city2 now city1 guy called the city2 guy in his city. now when city2 guy reached in city1 then he said to city1 guy that you called me so you will provide all the food and rent and living room stuff. City1 guys agreed and he provided all the stuff. After some months city2 guy feel he need one data entry guy so he searched and found a guy from city3 so he called the guy from city3 , now when city3 guy reached city1 he said to city2 guy that “that you called me so you will provide all the food and rent and living room stuff. City1 guys agreed.” now after few weeks city3 guy work end so now tell me one thing :

The room , rent and food equipment provided by city2 guy to city3 guy , now its time to return things back to city2 guy so city2 guy will return these things to whom ?

obvious that he will return whom provided these things means city2 guy now he returned all the stuff back to city2 guy and returned in his city3. Now after some months city2 guy work also done so now its time to return all the stuff which city1 guy provided to city2 , he returned all the stuff back to city1 guy and returned in his city2. now all the things have city1 guy who is main.

Now you can get idea that city1 , city2 and city3 guys are three functions.

Leave a Reply

Your email address will not be published. Required fields are marked *