FAQ: Introduction to Functions - Returns

Line 15 is assigning a None value to the variable, result since the called function has no return, so Python returns None. Technically, print() returns None so that is what gets returned, but same reason… print() doesn’t have a return value.

Thanks for feedback. Appreciate it. So if I get you right, if on line 4 in the code I had ‘returned’ rather than ‘printed’ the output string, this would not have happened? (I assume I cannot go back now to that exercise to check it out myself once I have moved on in the course)

Lines 15 and 18 are the ones returning None. The function is doing the printing so don’t print or assign the return value. There is none.

1 Like

Been busy crunching the Django web development course last days. Sorry for late reply on this thread. Anyway, I will let the advice “sink” and play around with the code. Cheers, mtf

1 Like

Are you actually able to use arguments in def funcs in such manner or is it just how the website is accepting the code?

First, would someone give better explanation of why & how return function works, when it’s used? I can see how it works in our code, but it doesn’t give me understanding of “how & why”. victoria-dr’s comment is concise, but doesn’t paint a whole picture of the function.
Second, why in this code two minuses give addition?
def deduct_expense(budget, expense):
return budget - - expense # gives you budget + expense.

Say we have a function to multiply two numbers…

def mul(a, b):
    return a * b

The function lives in Python’s global namespace. The parameters are in the local namespace, and the values live somewhere in memory that the variables point to.

Now let’s call the function…

c = mul(6, 7)

When Python does the multiplication the product is stored in memory. return knows where that is, AND it also knows where the caller is. Its natural behavior when exiting the function is to return to the caller. Well, not the caller exactly, but the very next point in memory where the next instruction is carried out. In the case above, the point of the assignment.

So what return has done is transfer what it knows about the value stored in memory back to global scope to where it is assigned to a variable. The value itself doesn’t move. It stays in memory where Python stored it. Return simply shares the location information with the caller so it can be accessed from caller scope.

Making sense?

same thing with the expense, was it replaced with shirt expense?

I’m still trying to understand the spacing myself. This could be the solution, though:

new_budget_after_shirt = deduct_expense( current_budget, shirt_expense )

Seeking further clarification on the spaces for line 14. I’m not sure when to use the spaces or why they are needed?

You don’t need spaces in the parentheses here, (current_budget, shirt_expense) would be fine so long as the two names are separated by the comma. So (current_budget,shirt_expense) would be also be acceptable as would ( current_budget, shirt_expense ). Spaces are often included for stylistic reasons though, normally for the sake of readability.

In Python you’d most likely see each argument separated by a comma (necessary) and a single space (optional): func(current_budget, shirt_expense) but some groups may use a different style and ignore them and add extras, it comes down to best judgement if not personal taste. I think for the most part a single space between functions arguments would be the best choice for now as it’s probably the most common (the lessons normally use this too).

Just be aware your previous post is replying to a post from over a year ago so the user has probably moved on. I think they were referring to indentation following the function definition, so the statements pushed forwards by spaces following def name(args): in this case just a return.

For anyone who curious about the indentation

The indentation would control which statements are part of the function definition, those that are part of it must be indented but you leave the definition by removing the indent.

def func(a, b):
    c = a + b  # part of the function
    d = c - 1  # part of the function
    return d  # part of the function

x = "bark"  # not part of the function

A recent reply about I made about indentation might help anyone else who was curious about indentation/grouping of statements-
FAQ: Control Flow - Else Statements - #23 by tgrtim

1 Like

In this example, as defined in the function, so even “exchange_rate” is a float and the “return” value can do the calculation without the need to int(exchange_rate)?

if i do it another way like below, may anyone let me know what is wrong? It looks like the calculation here is not working.

US_dollars=input()

exchange_rate=1.4

new_zealand_exchange= int(US_dollars)*int(exchange_rate)

print("100 dollars in US currency would give you " +str(new_zealand_exchange)+ " New Zealand dollars")