FAQ: Introduction to Functions - Scope

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

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

Computer Science
Data Science

FAQs on the exercise Scope

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

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

3 posts were split to a new topic: Can I call a function inside of a print function?

3 posts were merged into an existing topic: Can I call a function inside of a print function?

If we can directly call a function within print, what is the purpose of ‘return’?

If we don’t return anything then there is nothing to print (except None).

1 Like

Thanks. From the examples in the exercises, I have come to understand that we use return if our function contains any calculations and we need to use the numerical results later in our code; And, if the function contains a print statement, there’s no need for using ‘return’. Is that right?

It’s not a question of if, but what we plan to do with the result. When all we wish to do is print, then it’s okay to print inside the function. However a function is more useful (can be called from anywhere) if it has a return that can be printed at the caller. We can capture that value, which means we might not even want to print it, but use it in further computations.

def add(a, b):
    print (a + b)

The utility of this function is limited. What if we want to use the sum in a further computation?

def add(a, b):
    return a + b

Now we have options at the caller to either print, store and print, or just store.

print (add(6, 7))
# 13
c = add(6, 7)
print (c)
# 13

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

Now we can even use the previous call as an argument…

print (mul(add(6, 7), 4))
# 52
1 Like