Question about the return function

hey, I am brand new to coding and I have a question about the return function

Why do I have to use the return key under the define section code in order for my code to have an output value of 48?

Why do i get “none” as my output value when i use the don’t use the return function?


2 Likes

Umm, because that’s how Python, and all other languages, work…?

5 Likes

return does exactly what it sounds like. Returns a value, that can be used and assigned throughout your program. For example, your function multiplies two numbers together and returns the result. If your function only multiplied two numbers together and didn’t return anything, it would be like simply calculating the result but then immediately forgetting what that result was.

In Python by default, if your function doesn’t use return, it will simply return None automatically. However, in other languages (C++, Java), something known as a void function exists, which does not return any value at all.

7 Likes

Essentially, if you set a variable equal to your function, the variable will be equal to what you return.

For example, you have this:

x = multiply(6, 8)

The variable x will be whatever you return from the function. However, if you don’t have a return function in your function definition, it will automatically set x equal to None.

Hope this helps!

1 Like

Hey another question, if this is true then why am i able to define this function without using the return function?

You don’t need to use return in functions.

2 Likes

Because that function wouldn’t return anything important regardless, so it’s irrelevant in that case.

2 Likes

return is not a function.

1 Like

To give you a better idea of how methods (functions for classes, but basically irrelevant because they work the same way) and return works, let’s take these examples with a few of Python’s built-in datatypes.

First, let’s consider Python’s str type. (Just a string, "Hello world!") If we have a variable, hello = "Hello!", and you wanted to make everything uppercase, you would have to do something like the following:

hello = hello.upper()

Then, take Python’s list type. Let’s say we have a list, someList = [1, 2, 3] and we wanted to add a value to the end of the list. This could be done using someList.append(4).

Based on my previous post, can you guess which is a return function and which is a void function (technically they are methods, but that isn’t important)? str is a return function, and list is the void function. This is the case because list is immutable and str is not, but you will learn more about that later.

The main difference between these two is that str preforms an operation, and returns the value after said operation is complete. In this case, the .upper() method is converting the string to uppercase, but it isn’t actually reassigning the string to uppercase. It only returns the uppercase value of that string, which the programmer can reassign to the original variable, or use in some other purpose.

Notice that the list doesn’t do that. You will only need to call the method and your list is automatically updated with the appended version. That’s because the method actually adds the value directly to the original list, and it doesn’t return a value because the list was already updated.

I would highly recommend learning about mutable and immutable datatypes in Python, as this post will make much more sense afterward.

5 Likes

This isn’t C you know.

3 Likes

Well, they have the same principles. A void function simply doesn’t return anything, and even though None is returned by default, you probably aren’t gonna be assigning a variable to a function that only prints "Hello, world!"

1 Like

It’s written in and run using C so it kindof is, it’s important to understand the way high level languages are built up from lower level ones.

1 Like

I think the best way to understand this behaviour is to think about it like this: all functions return a value.

That’s actually technically what makes them functions as opposed to what some languages would call subroutines.

The point of a function is to take in some information, process it in some way, and give us some new information, or a result. Functions package functionality into components we can easily reuse.

A function keeps running, line by line, until a value is returned. When the end of the function is reached and no value is returned, to stop the function a value must be returned. In this case, a special value that represents nothing, or an absence of value, which in Python is None.

Hopefully that helps your understanding :smile:

4 Likes