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")

In this exercise

current_budget = 3500.75

def print_remaining_budget(budget):

print(“Your remaining budget is: $” + str(budget))

print_remaining_budget(current_budget)
When you hit run the first time why the variable budget take the value of current_budget? (Maybe this is a super stupid question)

I thought that the first run would print an error.

Thank you in advance

Above we use the global variable as the argument in the call to print_remaining_budget(). That function has one parameter, budget that receives the value given in the argument. The parameter is a local variable, only.

1 Like

I had the same question and after playing with the code a bit, here’s what I figured out:

current_budget = 3500.75 #global variable, visible to entire program since it's at the top def print_remaining_budget(budget): #Function #1 print("Your remaining budget is: $" + str(budget)) print_remaining_budget(current_budget) #passes global variable "current_budget" to print_remaining_budget(budget) which is a local variable for that function only # Write your code below: def deduct_expense(budget, expense): #Function #2 return budget - expense shirt_expense = 9 #Also global variable, but only visible to code below it new_budget_after_shirt = deduct_expense(current_budget, shirt_expense) #Same as above, "deduct_expense" function is passing 2 global variables for use in the function. The "new_budget_after_shirt" variable is then assigned the returned value. print_remaining_budget(new_budget_after_shirt) #Calls the "print_remaining_budget" function and passes the value of new_budget_after_shirt into the local budget variable for use in that function

Hope that helps.

2 Likes

I also dont understand this one as I dont see where/how budget is defined…???

One of the advantages of functions is that we can re-use them repeatedly without having to edit them.

If a function is supposed to be provided with some input(s), then we can invoke it with different inputs without having to modify the function. The values that we provide as input are known as arguments. We want our functions to be abstract enough that they don’t have to be edited to handle different arguments. Therefore, functions are defined with parameters. Whatever argument is passed to the function, it is assigned to the parameter. Within the body of the function, we use the parameter’s name to use the data provided as input.
The name of the parameter doesn’t have to match the name given to the argument. We don’t care about the name of the variable which holds the value being passed in to the function. Whatever it may be called outside the function doesn’t matter to us. Within our function, we will simply assign that value to our parameter. That way we don’t have to change our function for different inputs.

For example,

def print_remaining_budget(budget):
  print("Your remaining budget is: $" + str(budget))

Here, print_remaining_budget is the name of the function, and it has one parameter named budget. When the function is called with some argument, that argument is assigned to budget. Within the function, we use budget to work with this data. It doesn’t matter if outside the function, the data has been assigned to a variable whose name is different than budget. We are not constrained that the argument name must match the parameter name.

The following are all valid calls and don’t require us to make any changes to our function:

# 120.50 is the argument. It will be assigned to the parameter named budget.
print_remaining_budget(120.50)
# Output: "Your remaining budget is: $120.50"

# Outside the function, abc has been assigned value 98.52
# When function is called with this argument, 98.52 is assigned to parameter budget.
# Within function, we use the parameter budget to access provided value of 98.52
abc = 98.52
print_remaining_budget(abc)
# Output: "Your remaining budget is: $98.52"

If we provide more arguments than parameters, we will get error.

Similarly, if we provide fewer arguments than expected, we will get error (unless we have given some default values to the parameter(s)).

print_remaining_budget(32.40, 19.33)
# Error: Too many arguments

print_remaining_budget()
# Error: Too few arguments

Is it the website that has issues or I didn’t enter the code correctly cause this what I have been facing since…Firstly whenever I input my code into the learning area from the website it won’t display my result and still pop_up code error even if I enter the code correctly until I replace it with the solution code from get stock, can you please resolve this for me

Line 10 needs to indented (expected block).

what does that mean… i mean how am i going to unblock it? i really did not get it