Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is a Function? | What is a Function?
Python Functions: From Zero to Hero
course content

Contenido del 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

bookWhat is a Function?

Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.

A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.

123456
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
copy

Now, let's turn this code into a function.

123456
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
copy

We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called. To call a function, you have to use the name of the function followed by parentheses. Let's look at an example.

1234567
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
copy

When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.

Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.

Let's look at the syntax of a function in Python.

  1. In Python, the function is defined by def.
  2. Then the name appears (the name is any legal name of a variable).
  3. The name of the function is followed by parentheses.
  4. Then there is a colon.
  5. Then we have a code indent that is executed inside the function.
  6. To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.

It's time to practice.

Tarea

You have to implement a function named function, that displays data like this:

London is the capital and largest city of England and the United Kingdom.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1
toggle bottom row

bookWhat is a Function?

Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.

A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.

123456
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
copy

Now, let's turn this code into a function.

123456
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
copy

We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called. To call a function, you have to use the name of the function followed by parentheses. Let's look at an example.

1234567
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
copy

When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.

Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.

Let's look at the syntax of a function in Python.

  1. In Python, the function is defined by def.
  2. Then the name appears (the name is any legal name of a variable).
  3. The name of the function is followed by parentheses.
  4. Then there is a colon.
  5. Then we have a code indent that is executed inside the function.
  6. To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.

It's time to practice.

Tarea

You have to implement a function named function, that displays data like this:

London is the capital and largest city of England and the United Kingdom.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 1
toggle bottom row

bookWhat is a Function?

Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.

A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.

123456
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
copy

Now, let's turn this code into a function.

123456
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
copy

We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called. To call a function, you have to use the name of the function followed by parentheses. Let's look at an example.

1234567
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
copy

When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.

Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.

Let's look at the syntax of a function in Python.

  1. In Python, the function is defined by def.
  2. Then the name appears (the name is any legal name of a variable).
  3. The name of the function is followed by parentheses.
  4. Then there is a colon.
  5. Then we have a code indent that is executed inside the function.
  6. To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.

It's time to practice.

Tarea

You have to implement a function named function, that displays data like this:

London is the capital and largest city of England and the United Kingdom.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Functions are considered to be one of the most important elements of any programming language, hence Python is not an exception. A function is a block of code that performs some operations, and as a result of its work returns a certain value. We can do without functions, but functions make program development more efficient, easier to manage and understand.

A function is a piece of program code that can be accessed anywhere else in the program by calling its name. Using the functions will allow you to avoid code repetition. Let's look at an example. Let's say you want to calculate the value of your grocery cart and display the result.

123456
price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples + price_of_carrots + price_of_oranges print(total_price)
copy

Now, let's turn this code into a function.

123456
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price)
copy

We can see that there is a difference between the code at the beginning and this function: a function does something only when it is called. That is, if you run the function code written above, it will not return anything to you, because this function is not called. To call a function, you have to use the name of the function followed by parentheses. Let's look at an example.

1234567
def grocery_cart(): price_of_apples = 10 price_of_carrots = 17 price_of_oranges = 16 total_price = price_of_apples +price_of_carrots + price_of_oranges print(total_price) grocery_cart()
copy

When writing functions, keep in mind both the code that defines the function and the code that calls the function. If there is no of the parts, then nothing happens.

Note that the code of the function itself and the code calling the function does not have to be next to each other, there may be thousands of lines of code between them. However, the definition of a function MUST precede a function call.

Let's look at the syntax of a function in Python.

  1. In Python, the function is defined by def.
  2. Then the name appears (the name is any legal name of a variable).
  3. The name of the function is followed by parentheses.
  4. Then there is a colon.
  5. Then we have a code indent that is executed inside the function.
  6. To run a code that is inside a function, in other words, to call a function, we must write the name of the function, followed by parentheses.

It's time to practice.

Tarea

You have to implement a function named function, that displays data like this:

London is the capital and largest city of England and the United Kingdom.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 1. Capítulo 1
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt