Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Faris Rachmadyo
Faris Rachmadyo
1,660 Points

i give parameter to my function, but Mr Craig didnt, whats the different???

i had just learn about Display The List video, and i create function similar with Mr Craig instruction, but i add a parameter there (line 19 - 22) while Mr Craig did not add any parameter, whats the different? i test my code work well same with the video

sorry for my english, i am not native speaker

shopping_list = []
def add_to_list(item):

    shopping_list.append(item)

    print("you have just add -> ", shopping_list[-1])
    print("you have {} items in your shopping list".format(len(shopping_list)))

def show_help():
    print("""
    ---type HELP to get Help---
    ---type DONE to get done---
    ---type SHOW to get shopping list ---
    """)

print("What shoul we pick up for you???")
show_help()

def show_list(shop_list):
    print("what you buy is ")
    for item in shop_list:
          print("> ",item)

while True:
    new_item = input("< ")
    if new_item.lower() == "done":
        break

    elif new_item.lower() == "help":
        show_help()
        continue

    elif new_item.lower() == "show":
        show_list(shopping_list)
        continue

    add_to_list(new_item)```

3 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,797 Points

The parameter is a placeholder variable for adding the shopping list as an argument, when the show_list function is called. It's the way that the information is given to the function so it knows what to display to the terminal. :-)

Hey Faris, I agree with Jonathan above. If a parameter wasn't passed to the function, the code within the function is just performing an action on global variables available within the code. If Mr. Craig didn't include a parameter, that's likely an because his code is simply modifying a predefined variable. Generally speaking, every function should have a parameter, although some parameters can be optional UNLESS the purpose of the function is simply to modify global variables. Generally speaking, it may be best practice to add a parameter, assign the response of the action on the variable to a new one, and then return that variable (which makes the scope of the variable local instead of global). This is done to prevent the global variable from being modified. Here's an example:

my_shopping_list = ['tomatoes', 'apples', 'pears'] #define a new shopping list

def remove_item_from_list():
   my_shopping_list.pop() #remove the last item from the shopping list
   return my_shopping_list #return the global shopping list value

Here's what happens in the console

>>> print(my_shopping_list)
['tomatoes', 'apples', 'pears']
>>> remove_item_from_list()
['tomatoes', 'apples']

Considering the above, let's use another example:

my_shopping_list = ['tomatoes', 'apples', 'pears'] #define a new shopping list

def remove_item_from_list(my_shopping_list):
   a = my_shopping_list
   x = a[1]
   return a, x

Here's what happens in the console

>>> remove_item_from_list(my_shopping_list)
(['tomatoes', 'apples', 'pears'], 'apples')
>>> my_shopping_list
['tomatoes', 'apples', 'pears']

You can see that the original array of shopping list isn't impacted with this code, but it does perform an action on it. You could do the same with or without the parameter, but the value of x remains present only within the function do to local scoping and garbage collection. If you try to call the variable x outside of the function, you get this:

>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

Hope that's helpful!

Faris Rachmadyo
Faris Rachmadyo
1,660 Points

thanks a lot for the explanation, now its clear to me

Thanks Faris! Could you please mark my response as an answer to your question? Keep coding!

Faris Rachmadyo
Faris Rachmadyo
1,660 Points

hey Fergus, i dont know how to mark your response to be an answer, i cant click it to be best answer or add comment both to your response