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
2 Likes

Okay, why DOESN’T the example function work/ how can you make it work? I’m referring to:
def create_special_string(special_item):
return "Our special is " + special_item + “.”

print("I don’t like " + special_item)

My attempt at fixing it was:

special_item = grapes

def create_special_string(special_item):

return "Our special is " + special_item + “.”

print("I don’t like " + special_item)

print(create_special_string)

and that didn’t work, says I did not define ‘grapes’. I also tried the same except with print (create_special_string(grapes)) and it gave the same error that I had not defined grapes. I feel I need to grasp this to really understand the lesson, but I don’t understand why it doesn’t work.

Hello, @java8100890366, and welcome to the Codecademy Forums!

In your code, grapes does not have quotes around it, so the Python interpreter regards it as an undefined variable rather than as a string.

Please see How to ask good questions (and get good answers) for advice on how to format code for posting. Formatting your code will make it easier for users to read.