Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arguments or Parameters | Arguments or Parameters
Python Functions: From Zero to Hero
course content

Conteúdo do Curso

Python Functions: From Zero to Hero

Python Functions: From Zero to Hero

1. What is a Function?
2. Arguments or Parameters
3. Assigning a Default Value to a Parameter
4. An Unknown Number of Arguments
5. Functions within Functions
6. Recursion

bookArguments or Parameters

Pay attention to the parentheses, when creating a function def grocery_cart() and when calling a function grocery_cart(). They should not be empty.

The data you specify in parentheses is passed to the function so that the function can use it during its execution.

Let's see how it works! Suppose we call the grocery_cart(10, 7, 12) function instead of grocery_cart(). That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(10, 7, 12)
copy

We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.

Parameters or arguments?

A parameter is a variable that we specify in parentheses when we define the function.

An argument is a value that we specify in parentheses when calling the function.

Arguments in the function call - numbers 10, 7 and 12. Parameters in the definition of the function -- price_of_apples, price_of_carrots and price_of_oranges. Accordingly, these variables contain the numbers that are arguments, ie: 10, 7 and 12, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples is 12 price_of_carrots is 8 price_of_oranges is 9. Therefore, in this case, we have the following piece of code:

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(12, 8, 9)
copy

But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12 is the first argument, it passes to the first parameter, price_of_apples. Since 8 is the second argument, it passes to the second parameter price_of_carrots. Since 9 is the third argument, it passes to the third parameter price_of_oranges.

The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.

Tarefa

You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.

To test, take these values as arguments height = 5, width = 23

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
toggle bottom row

bookArguments or Parameters

Pay attention to the parentheses, when creating a function def grocery_cart() and when calling a function grocery_cart(). They should not be empty.

The data you specify in parentheses is passed to the function so that the function can use it during its execution.

Let's see how it works! Suppose we call the grocery_cart(10, 7, 12) function instead of grocery_cart(). That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(10, 7, 12)
copy

We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.

Parameters or arguments?

A parameter is a variable that we specify in parentheses when we define the function.

An argument is a value that we specify in parentheses when calling the function.

Arguments in the function call - numbers 10, 7 and 12. Parameters in the definition of the function -- price_of_apples, price_of_carrots and price_of_oranges. Accordingly, these variables contain the numbers that are arguments, ie: 10, 7 and 12, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples is 12 price_of_carrots is 8 price_of_oranges is 9. Therefore, in this case, we have the following piece of code:

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(12, 8, 9)
copy

But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12 is the first argument, it passes to the first parameter, price_of_apples. Since 8 is the second argument, it passes to the second parameter price_of_carrots. Since 9 is the third argument, it passes to the third parameter price_of_oranges.

The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.

Tarefa

You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.

To test, take these values as arguments height = 5, width = 23

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
toggle bottom row

bookArguments or Parameters

Pay attention to the parentheses, when creating a function def grocery_cart() and when calling a function grocery_cart(). They should not be empty.

The data you specify in parentheses is passed to the function so that the function can use it during its execution.

Let's see how it works! Suppose we call the grocery_cart(10, 7, 12) function instead of grocery_cart(). That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(10, 7, 12)
copy

We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.

Parameters or arguments?

A parameter is a variable that we specify in parentheses when we define the function.

An argument is a value that we specify in parentheses when calling the function.

Arguments in the function call - numbers 10, 7 and 12. Parameters in the definition of the function -- price_of_apples, price_of_carrots and price_of_oranges. Accordingly, these variables contain the numbers that are arguments, ie: 10, 7 and 12, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples is 12 price_of_carrots is 8 price_of_oranges is 9. Therefore, in this case, we have the following piece of code:

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(12, 8, 9)
copy

But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12 is the first argument, it passes to the first parameter, price_of_apples. Since 8 is the second argument, it passes to the second parameter price_of_carrots. Since 9 is the third argument, it passes to the third parameter price_of_oranges.

The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.

Tarefa

You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.

To test, take these values as arguments height = 5, width = 23

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Pay attention to the parentheses, when creating a function def grocery_cart() and when calling a function grocery_cart(). They should not be empty.

The data you specify in parentheses is passed to the function so that the function can use it during its execution.

Let's see how it works! Suppose we call the grocery_cart(10, 7, 12) function instead of grocery_cart(). That is, we passed some information to the function as arguments. Each of the numbers in parentheses is an argument. Arguments that you pass must be separated by commas. Arguments ensure the versatility of the function. After all, in this case, you can use any numbers, not just those that are hard-wired into the function itself, as variables. Let's look at the example.

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(10, 7, 12)
copy

We see that we have now inserted the data in the function call parentheses as well as in the function definition parentheses. Namely, the function call strings contain three numbers (10, 7, 12), and the function definition parentheses now contain three variables (price_of_apples, price_of_carrots, price_of_oranges) that store data. These variables store the data passed by the calling code, 10, 7 and 12.

Parameters or arguments?

A parameter is a variable that we specify in parentheses when we define the function.

An argument is a value that we specify in parentheses when calling the function.

Arguments in the function call - numbers 10, 7 and 12. Parameters in the definition of the function -- price_of_apples, price_of_carrots and price_of_oranges. Accordingly, these variables contain the numbers that are arguments, ie: 10, 7 and 12, and accordingly, these variables are used in the body of the function. Our function can take any three numbers as arguments. So let's go to another store where the prices are different, and accordingly, price_of_apples is 12 price_of_carrots is 8 price_of_oranges is 9. Therefore, in this case, we have the following piece of code:

1234
def grocery_cart(price_of_apples, price_of_carrots, price_of_oranges): total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart(12, 8, 9)
copy

But how does Python know that 12 is included in the parameter responsible for the price of apples, not oranges, and so on? Simply: since 12 is the first argument, it passes to the first parameter, price_of_apples. Since 8 is the second argument, it passes to the second parameter price_of_carrots. Since 9 is the third argument, it passes to the third parameter price_of_oranges.

The number of parameters we pass in the function definition must be equal to the number of arguments to be passed when calling the function.

Tarefa

You have to implement a function that accepts 2 positive integers which are the height and width of a rectangle and returns a dictionary that contains the area and perimeter of that rectangle.

To test, take these values as arguments height = 5, width = 23

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 1
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt