FAQ: Introduction to Functions - Defining a Function

This community-built FAQ covers the “Defining a Function” 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 Defining a Function

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!

I followed all the directions but nothing is displayed on the console, what did I do wrong? I attach my code.

Your code below:

def directions_to_timesSq():
print(“Walk 4 mins to 34th St Herald Square train station”)
print(“Take the Northbound N, Q, R, or W train 1 stop”)
print(“Get off the Times Square 42nd Street stop”)

directions_to_timesSq()

Is this your exact code? If so, the issue is most likely that the body of the function (all of the prints, in this case) isn’t indented.

1 Like

Make sure you name the function directions_to_timesSq() instead of directions_to_times_square() like I initially did. Otherwise it will not let you move on to the second step. This may seem obvious, but for whatever reason it did not register with me because my brain parsed “timesSq” as being “times_square” which the exercise didn’t like.

def directions_to_timesSq():

print(“Walk 4 mins to 34th St Herald Square train station.”)

print(“Take the Northbound N, Q, R, or W train 1 stop.”)

print(“Get off the Times Square 42nd Street stop.”)

#Call your function here:

directions_to_timesSq() this is my code but am getting this error File “travel.py”, line 1
def directions_to_timesSq():
^
IndentationError: unexpected indent

1 Like

I have done both steps here, and even have a colen at the end of the def. But I can’t get past the first instruction and move on to the next page as it remains a “Red X” rather than a “Green checkmark”

print('Hello world!') def directions_to_timeSq():

‘Parenthesis’ should be ‘Parentheses’ in context because it is plural. Also, correct code only passed when I removed a line of comments. Please can someone fix this page a bit? A fuzzier point is that ‘Function header’ is introduced without making it crystal clear that the function header is the whole line; I am still in a bit of doubt about that…

Hello!

I finished the exercise for defining functions and am a bit confused about how the print statements become visible. The final hint reveals that you make the print statements visible by displaying the function as a variable.

Why is it possible to type out the variable to reveal the outcome when, in prior exercises, you’re expected to enter “print(variable)”? What allows you to bypass that step?

word = "Hello"
print(word)
# Output: "Hello"

In the above snippet, word is a variable. A string has been assigned to this variable. If we want to print this string, we pass the variable as an argument to the print function.

def word():
    print("Hello")

In this snippet, word is not a variable but the name of a function. The def keyword, the parentheses () after the function name and the colon : tell us that we are defining a function named word and not declaring a variable.

Even though we have defined a function, the statements in the body of the function (the print("Hello") statement in this example) are not being executed. Nothing is being printed. Why?

Think of defining a function as writing a recipe in a cookbook. A recipe in a cookbook is useful in the sense that it tells us what ingredients are needed and exactly what steps are needed to cook the recipe. If I have written a recipe for tomato soup (defined a function), then anytime I need to look up how to cook the soup, I just have to read the recipe. I don’t have to write the recipe from scratch. I can just look it up in the cookbook. BUT, a recipe in a cookbook is just that. Some text and pictures and steps written in a book. Practically, nothing is happening.

If I want to eat tomato soup, I have to tell someone to cook the recipe (or cook it myself). Now, a cook is going to actually carry out the steps of the recipe. He/she/I will slice and dice the tomato, boil stuff and so on. Telling someone to cook the recipe is similar to making a function call.

A function call is made by writing the name of the function followed by a set of parentheses (Note the def keyword and colon : are not being used to make a function call).

#
# Defining a function
#
def word():
    print("Hello")

# Calling a function
word() 
# Output: "Hello"

When I make a function call (tell the cook to start cooking), the statements in the function definition (the recipe) are executed.

Suppose the function definition was:

def word():
    return "Hello"

word()
# 
# Function is executed but nothing visible happens because the function
# body doesn't include a print statement. Instead, the function is returning
# a value (You will learn about return later in the course) 

print(word())
# Output: "Hello"
# A function call is made to the word function. The result is then passed as
# the argument to the print function.

x = word()
print(x)
# Output: "Hello"
# A function call is made to the word function. 
# The result is assigned to the variable x.
# Then the variable is used as the argument to the print function.
3 Likes

I get it now!

Thank you so much for your explanation!

1 Like

Can someone please explain me this?
def directions_to_timesSq():
print(“Walk 4 mins to 34th St Herald Square train station”)
print(“Take the Northbound N, Q, R, or W train 1 stop”)
print(“Get off the Times Square 42nd Street stop”)
It was mentioned that this piece of code doesn’t return anything on the output console. But under hint in the second step ,it says that make a call to your function like this “directions_to_timesSq()”. What does it mean? How to make a call to a function?

# Function definition. Note the use of def keyword and the colon :
def directions_to_timesSq():
  print("Walk 4 mins to 34th St Herald Square train station")
  print("Take the Northbound N, Q, R, or W train 1 stop")
  print("Get off the Times Square 42nd Street stop") 

# After and outside the function definition, a function call is made.
# This executes the statements in the body of the function.
# Note the lack of def keyword and colon : in the function call.
directions_to_timesSq()

The instructions mention,

Remember, if you run your code, you shouldn’t see any output in the terminal at this point.

Printing is not synonymous/interchangeable with function returns.

In the exercise, we are just writing the function definition, so nothing happens.

If we add a function call after the definition, then the statements inside the function’s body are executed and strings are printed to the screen. This is known as calling or invoking the function.

Thank you for the reply. I understood now.

@sherrymuriuki in Python functions the indents must be taken seriously. Like loops and conditionals, code inside a function must be indented to show that they are part of the function. Your code pasted here doesn’t show any indents. Unindented lines following function definition will not be included in the function.

@css0001239804 not sure if you are still stuck but also for the benefits of others - your function name has to have exact match with the lesson for you to move on.

Here, your function name is directions_to_timeSq() - the lesson calls for directions_to_timesSq()

subtle difference but it got me too.

what is it doing when it is ‘parsing’?
what does it mean in this context?(i assume it is similar to compiling, but i have no idea)

Output-only Terminal
Output:

File “travel.py”, line 2
directions_to_timesSq():
^
SyntaxError: invalid syntax
(the arrow is actually pointing to the colon, but for some reason codecademy doesn’t let you just comment this)
uhhh, it says: “don’t forget the colon!”
but i didn’t, bug in the software?
is it even parsing or has it failed altogether?

there is no difference, is this something i can check in an equality statement?
print(“directions_to_timeSq()” == “directions_to_timesSq()”)
edit: i just went into the ‘stuck, get a hint thing’ and found ‘def’ apparently i forgot that

@thedispatcher

Hi! In the context of the exercise there is a difference. The exercise stated clearly to create a function called directions_to_timesSq(). For @css0001239804 not having the exact same wording as part of the lesson check would not get him the green checkmark.

Logically there is no difference in the sense that whatever you name the function is up to you and as long as you are consistent in your call then it doesn’t matter.

In a working environment though, there is something to be said about following instructions. Maybe you are working on a larger code block as part of the team and other people are actively calling the function you created or referencing it. Maybe you are maintaining the code someone else created and in these situation when people tell you to “create a function that’s called X” you better be very diligent on making sure your X is spelt and has the parameters exactly the same as what other people are asking you to do. It’s a good practice to get onboard early and be mindful of spellings.

Parsing is part of the compiling process, it’s when the code is analyzed by the software and broken down so the instruction can be followed, so you are somewhat correct.

Looks like you figured out your error! Great job!

1 Like