FAQ: Introduction to Functions - Types of Arguments

This community-built FAQ covers the “Types of Arguments” 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 Types of Arguments

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 #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

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 #community:Codecademy-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 can not complete step 6. When I switch to “view solution” in this particular step the solution shows the following.
I submitted a bug report but I’m not sure if I explained it correctly.
Follow a picture (is this a bug?):

1 Like

I’m pretty new to this but I think I have it right:
I stumbled on step 6 initially but the difference with step 6 vs the preceding 2 steps is that we are wanting to move away from the order we defined in line 2 of first-second-final destination.

Step 6 asks us:

Call the function trip_planner() again using keyword arguments in this exact order:

  1. first_destination: "Iceland"
  2. final_destination: "Germany"
  3. second_destination: "India"

We can see that the order has changed from first-second-final to first-final-second. If you scroll up to the intro before the task on the left side, you will see the explanation about keyword argument which is what we are doing here because the way you have typed it in line 9 would attribute Iceland as first destination (correct), Germany as second and India as second, both of which are incorrect as we are wanting to switch the order.

I’m not completely sure as to the utility of this and what order they would print in.

1 Like

Does your final output look like this?
“Here is what your trip will look like!
First, we will stop in France, then Germany, and lastly Denmark
Here is what your trip will look like!
First, we will stop in Denmark, then France, and lastly Germany
Here is what your trip will look like!
First, we will stop in Iceland, then India, and lastly Germany
Here is what your trip will look like!
First, we will stop in Brooklyn, then Queens, and lastly Codecademy HQ”

1 Like

Well explained, thanks!
I was actually able to understand my error later on. As you said, the purpose is for us to apply what we learned - set value to function argument. It is not relevant at this stage but later on, when utilizing multiple arguments kind of make sense, feels useful.

For anyone who is stuck on Exercise 6 like I was a keyword argument is the argument written in when you define the function. The solution would be

def trip_planner(first_destination = "Iceland", second_destination = "India", final_destination = "Germany"):
2 Likes

I was thinking the same thing when i wrote it like you did, but i believe its not a bug they probably wrote in their backend something like if bla bla == bla bla then pass is True, but since you wrote it like the above exercisement(assignment 4 and 5) it was returning a false hence the fail.
What there intention was for this exercise was that you implemented the following arguments.
trip_planner(first_destination = “Iceland”, final_destination = “Germany”, second_destination = “India”)
this lies in the word they used:
in this exact order:

1 Like

I got the exercises right but my question is how would it look like if I have defined, for example, the second parameter in the definition but not the first and third and then I call the positional argument for only the first and third

would it be something like this?

def trip_planner(parameter1, parameter2 = x, parameter3) trip_planner(argument1, ,argument3)

I hope my question is clear, as I am not a native english speaker

Hey @jorgerodriguez707979

I think I understood what you’re trying to say here. You’ve passed an argument (i.e assigned a value) to one of the parameters, but not to the other two.

In thise case, Python would throw an error, because while defining the function in itself, you did not assign any default values to the other parameters, and Python did not have any value to fallback on - and hence not completely run the function. Here’s an example, where I had defined a default value for argument3, but did not pass all the values.

[codebyte]

1 Like

Hi All,

Need some help here. Here’s a codebase.

def trip_planner(first_destination, second_destination, final_destination = "Codecademy HQ"): print("Here is what your trip will look like!") print("First, we will stop in "+first_destination + " then "+second_destination + ", and lastly "+final_destination) trip_planner("France","Germany","Denmark")

Here’s my question:

While defining my function, I’ve given final_destination a value (Here, = “Codeacademy HQ”) which is default, and something that can be picked up if there’s no argument passed for it in subsequent steps.

Now you’d see later, that using the concept of positional argument, I have indeed passed the argument of “Denmark” as the value of final_destination (since both are 3rd in their lists) - However, how is Python smart enough to know that it is an argument for a parameter which is just final_destination?

You see, until I had defined the function, Python didn’t have knowledge of that parameter. Now that I’ve given it, how is it smart enough to know that the parameter is just “final_destination” and not `final_destination = “Codeacademy HQ”’

This is a little basic question as I’m really trying to understand how a computer is smart enough to differentiate that it can accept another value that is different (Denmark) from a value that is assigned to it when it was defined in the first place.(Codeacademy HQ)

Hope I make sense.

The docs are always great for a little more info- https://docs.python.org/3/tutorial/controlflow.html#default-argument-values

Default arguments act more like a fallback, if that parameter is not passed as an argument in a call to the function then the default is used. Under the hood just a little that default value "Codecademy HQ" is actually saved as part of the function object. The parameter name is separate to this value, if an argument is passed to this parameter then that object is assigned to that name in the function, if not then the default is assigned (this occurs on every new call).

Bear in mind that Python is high-level language. There are quite a few levels of abstraction between the code you write and the instructions finally sent to the machine. So it’s not necessary the computer making the choice, it’s the Python interpreter which is itself software that has been written to act in a certain way.

Hi Jorge,

What you have done here is provide the function with a default argument. What your code will do is print out those three locations by default if no argument is provided when the function is called, i.e. trip_planner(). While it works to return a correct answer, it’s not providing a keyword argument.

Providing a keyword argument would look like this:

def trip_planner(first_destination, second_destination, final_destination = "Codecademy HQ"):
  print("Here is what your trip will look like!")
  print("First, we will stop in " + first_destination + ", then " + second_destination + ", and lastly " + final_destination)

trip_planner(first_destination="Iceland", final_destination = "Germany", second_destination = "India")

Notice how I’m calling the the function on the 5th line after it’s been defined above. If I left first_destination or second_destination blank it would result in an error, but if I leave final_destination blank it will default to “Codecademy HQ”

my output has been without spaces despite following the code, using the + sign.
First, we will stop inLondon, thenIndia, and lastlyNew Zealand
Here is what your trip will look like!
First, we will stop inFrance, thenGermany, and lastlyDenmark

Why is there no space as a result ? what am i missing?

def trip_planner(first_destination, second_destination = "Codecademy HQ", final_destination)
 File "travel.py", line 2
    def trip_planner(first_destination, second_destination = "Codecademy HQ", final_destination):
                    ^
SyntaxError: non-default argument follows default argument

Hi, I experimented with letting the second parameter having the default argument instead of the last parameter, and I got this error. Does this mean that only the last parameter can have the default argument?

1 Like

It’s not strictly the last parameter (since you can have multiple positional parameters and multiple default parameters), you just need to define all the normal positional parameters (no defaults) before any default parameters.

def f(a, b, c, d='d'): pass  # fine
def f(a, b='b', c='c', d='d'): pass  # fine
def f(a='a', b, c, d): pass  # not valid, error thrown

Ohh I see… thanks for the clarification :slight_smile:

1 Like