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

Course Content

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 distance between New York and Boston and display the result.

12345
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
copy

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

12345
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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.

123456
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
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 call to the function, 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 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 distance between New York and Boston and display the result.

12345
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
copy

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

12345
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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.

123456
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
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 call to the function, 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 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 distance between New York and Boston and display the result.

12345
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
copy

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

12345
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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.

123456
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
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 call to the function, 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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 distance between New York and Boston and display the result.

12345
distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
copy

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

12345
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance)
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.

123456
def calculate_total_distance(): distance_new_york_to_boston = 215 distance_boston_to_philadelphia = 300 total_distance = distance_new_york_to_boston + distance_boston_to_philadelphia print(total_distance) calculate_total_distance()
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 call to the function, 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.

Task

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 desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt