How exactly do we need to indent when it comes to the "return" function


#1

Hello, for a while now, I have had quite a lot of problem with the “return” function. Sometimes we need to indent outside and sometimes there needs to be no more indentation after the “for” function but sometimes there needs to be. I really don’t understand this and it’s been bugging me with quite a few exercises. Any clarification would really help me.


#2

The return function returns some value or dataset from the function back to the code that called the function.

Functions are our workers. They do stuff. We call a function to do some task such as sort, compute, check a state, run some calculation, get information from the user or database, or whatever.

Many times, we need the function do gather information or run calculations and then return the results to us for further processing.

  • “Hey, function go get some information from the database and give it back to me.”
  • “Hey, function take this set of numbers and run some calculations on it for them. Then give me back the results.”

Does that make sense to you?

We build a function to wrap up some task we want done. Why? So we can call that task over and over again. Subroutines (functions and methods) give us that ability.

Indentation and When to return

Some languages use curly braces or tags to define the opening and closing of a codeblock. For example, JavaScript, PHP, C#, and many others use {} to define that the code within those curly braces belong to that gatekeeper.

A gatekeeper is a function, if, else, else if, while, do, for, etc. These instructions control whether the codeblock runs or not.

In Python, we use indentation instead of opening and closing tags. Code that is indented below a gatekeeper instruction becomes part of its codeblock.

Still with me?

When to Return

You want to return from a function when you’re done processing. That return can happen anywhere within the function itself. It can be at the end of the function as the last line. It can be within an if or for loop.

You return and bail out (leave) the function when you’re done processing. There’s nothing else you want that function to do.

Wrap

If you are using a language like Python where indentation matters and you want to return out of the function at some point in an if or for loop, for example, then the return is indented within that codeblock:

def some_function(some_dataset):
    total = 0
    
    for datapoint in some_dataset:
        if datapoint < 0:
            return datapoint
        
        total += datapoint
        
    return total

Notice how the first return is indented within the for and if. Do you see that? It’s part of the loop and if the datapoint is less than 0, then return that datapoint and stop the loop and function from running.

The last return says that I want to return some value or data after the function has completed, i.e. if it completes.

You decide where to put the return based upon what you want your function to do.

Think about it. You are building the function to do some task. There are points in processing when you loop, check a state, and then determine “Hey, that’s the value I want or some condition isn’t right to continue.” So you bail out. You decide that.

What do you think? Does that help you?


#3

Yes, thankyou, I think I get it now :slight_smile: