FAQ: Introduction to Functions - Returns

This community-built FAQ covers the “Returns” exercise from the lesson “Introduction to Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Visualize Data with Python
Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise Returns

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Under the defined function, deduct_expense, I typed everything with indentation that didn’t seem to work. So when may I use indentation for the defined function?

In the example below, why must a str() be used in the print() line at the bottom? We weren’t using str() when we printed variables before.

Because you’re concatenating two strings with new_zealand_exchange which is likely to be a float or integer depending on what you pass to that function.

You can try outside your function if you like for what happens when you do this, e.g. x = "test" + 3 and you’ll see a type error.

2 Likes

‘Budget’ gets referenced several times within the functions but I fail to see how this ties back to the initial variable of ‘current_budget’ and it’s value of 3500. In this instance does line 15 simply replace line 6?

7 Likes

Did you ever figure this one out? I’m doing this at the moment, had the exact same thought and I cannot for the life of me figure it out

Hi @emziicles. No, not 100% but having observed how Python works I’ve concluded that the first instance of ‘budget’ was declared on line 6 which returns a string and that ‘deduct_expense’ on line 9 continues to carry this through. ‘current_budget’ on line 13/14 then swaps with ‘budget’ on line 9.

What I learned is the argument (current_budget) and (budget) are interchangeable
current_budget = 3500.75
shirt_expense = 9

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

print_remaining_budget(current_budget)

def deduct_expense(current_budget, expense):
return current_budget - expense

new_budget_after_shirt = deduct_expense(current_budget, shirt_expense)

print_remaining_budget(new_budget_after_shirt)

Two different ways of doing it.

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

print_remaining_budget(current_budget)

def deduct_expense(budget, expense):
return budget - expense

new_budget_after_shirt = deduct_expense(current_budget, shirt_expense)

print_remaining_budget(new_budget_after_shirt)

1 Like

Here’s another way to do it, using currency conversion
to keep your budget in USD (assuming 1 NZD = 0.7 USD as of today):

current_budget = 3500.75

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

# Write your code below: 
def deduct_expense(budget, expense, exchange_rate):
  return budget - (expense * exchange_rate)

shirt_expense = 9.95

new_budget_after_shirt = deduct_expense(current_budget, shirt_expense, 0.7)

print_remaining_budget(new_budget_after_shirt)
1 Like

Sooo, I’ve been thinking on the question about “budget” vs. “current_budget” conundrum - it almost seems like the “budget” and “expense” parameters initially passed in the print_remaining_budget and deduct_expense function definitions (lines 3, 4, 10 and 11 in my screenshot) are almost . . . placeholders? Like they serve no purpose, and the real parameters (or perhaps arguments? Sorry, I’m suuuuper new to this) aren’t used until the function is actually CALLED (lines 6 and 13)? Can anyone more knowledgeable than me confirm or deny? TIA!

1 Like

Sorry for a delayed reply, you are effectively correct, they do act like that. They are names (limited to the scope of the function) that have yet to be assigned to an object (the exact implementation is largely unimportant). Parameters are unimportant outside the function but with an obvious purpose inside the function.

At some point you may come across namespaces which will make this a little clearer.

When you call your function you pass your arguments, the objects these arguments reference are then assigned to the parameters inside the function, the function executes, and the assignment is forgotten once the function completes.

def func(value):  # value is our parameter
    return value

func(3)  # here an integer 3 is our argument
# an integer object, 3, is assigned to the name 'value' for the duration 
# of the function and this assignment is forgotten once the function exits
1 Like

Hi all,
I’d like to ask this since I’ve been looking at this page for days now and still wonder about this point:
We’re given a current_budget = 3500.75 at the beginning. Then our current_budget becomes just budget, though! Why is that?

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

print_remaining_budget(current_budget)

I must say I haven’t been working consistently during the last month and it could be a silly question… My apologies if that’s the case. And thanks for your help anyway!

4 Likes

It’s a parameter of that function, that is, a name which only exists in the scope of that function. By passing current_budget as the argument you are assigning budget to the value (the object really) referenced by current_budget for the duration of the function. It’s not overly different from doing the following-

current_budget = 10
def func():
    budget = current_budget
    print(budget)

You’ll likely come across more detail on functions as the course goes on but you could equally write print_remaining_budget(budget=current_budget) for your last line for the same result (this would be using it as a keyword argument, you’ll run into them eventually).

That name budget is limited to the function and is assigned when the function is called and forgotten again when the function exits.

Hopefully that makes it a little clearer.

5 Likes

This was very, very helpful. THANK YOU!

1 Like

you are correct, I don’t know what is the problem. See if you can find it your own, or use help from a parent!

For me it’s an issue within the exercise : It seems like the correction of the exercise wants deduct_expense function’s first parameter to be named current_budget and not budget … for me it’s a mistake as it can be misleading with the global function current_budget (i would let each local functions to use budget). So if there is any kind moderator that wants to look into this that would be much appreciated.

The only issue in using parameter names that are the same as globals is the shadowing it does to the global.

def foo(a):
    print (a)

a = 42

print (a)    #  42
foo(73)      #  73

Should we wish to access the global variable within the function we will need to address the outer scope…

print (a)
print (window.a)

I’m still a little bit confused as to what the purpose of the return function is, or when to use it. Why can’t the “calculate_exchange_usd” or “def deduct_expense” functions be used without “return”?

1 Like

Welcome to the forums!

You can write those functions without using return, but returning values is a handy way to pass useful information back to the function call. Say you needed to perform some set of mathematical operations on multiple numbers and that you also needed to store the results of the new numbers. By using return in this case, you would be able to store the new numbers in variables by using something like:

result1 = do_math(7.9)
result2 = do_math(0.98)
1 Like


Couldn’t figure out why terminal gives me the ‘None’ lines. (I have added some numbering 1 and 2 to the two various ways of printing the result, just to see any difference). Much appreciated if anyone knows.