Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

oz izhak
oz izhak
1,940 Points

Hi there, i have a newby question but i don't to ask if understand better python code.

when i write a new function like: def new_item() the function name is new_item right? so why sometime i see code that write between the brackets

i thing my question is what is the purpose of the word in the brackets?

tnx , Oz.

3 Answers

andren
andren
28,558 Points

The word(s) within brackets (usually referred to as parenthesis in programming) are parameters for the function. A parameter is something that allows you to pass data to the function when you call it.

Let's say you wanted a function to return the sum of two numbers, but you wanted these two numbers to be passed in to the function, instead of something that was defined in the function. That would be achieved by defining two parameters and then passing in two values (called arguments) to the method when you called it.

Like this:

def add(a, b): # a and b are parameters
    return a + b # a will equal the first argument passed in, b will equal the second

add(5, 2) # 5 will be passed in as the first argument, 2 as the second.
# The result of the method will be 7, since 5 + 2 equals 7.

Parameters and arguments allow you to create dynamic function whose result can differ depending on the data passed in to them, which makes them more reusable.

oz izhak
oz izhak
1,940 Points

So if I call the function few lines later like this:

add()

All the argument will work? Or i must call the function with all the arguments?

andren
andren
28,558 Points

You must call the function with the arguments. They are not provided automatically, if you called the add function in my example above without arguments then the code would crash since there wouldn't be any value assigned to a and b within the function.

oz izhak
oz izhak
1,940 Points

Uderstood In the shopping list code i had some problem with this. Because every time i saw him create some function and use the parameter in another function

I thing i'll return the pyrhon basic material to understand more ..