Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What Is Function? | What is Function in Python?
Python Functions Tutorial

What Is Function?What Is Function?

In Python, a function is a named block of reusable code that performs a specific task. It is a fundamental concept in programming and plays a crucial role in organizing and structuring code. Functions are like building blocks in LEGO. They allow you to create reusable chunks of code that can be used in different parts of your program.

  • Firstly, it's convenient because you can call the same block of code multiple times without having to rewrite it. This saves time and reduces the chance of making mistakes;
  • Secondly, functions help break down complex tasks into smaller, understandable steps. Each function can perform a specific action, making the code more readable and easier to manage.

In simpler terms, functions are like breaking down tasks into smaller pieces that you can use over and over again. This approach makes your program more convenient to write, change, and understand.

Defining a function

Let's to define the most simple function in Python.

  1. Firstly, the def keyword has to be used;
  2. Secondly, we have to specify the function name - a unique identifier that represents the function. It follows variables' naming conventions, such as using lowercase letters and underscores ( this convention is called snake_case);
  3. We have to use (): after the function name - in these brackets, we specify the function's arguments (parameters);
  4. Then, we specify the function body with the indented new line;
  5. After the function body, we can specify the return value of the function using different keywords such as return, assign, or yield;
  6. Finally, we can use functions in code. To do so, we have to call function using its name and specifying its parameters.

Let's look at the example. We will create a simple function to print 'Hello, world!' in the console and use this function in code by calling it.

Code Description
You may have noticed that we used print() function as the body of print_hello(). It is a built-in function that prints the text in the console. It has a string as and arguments - we specify this string in the () brackets.

You may ask why this function has no return value if it prints the message in the console.
In Python, a function can perform a task (in this case, printing a message) without necessarily returning a value. The presence or absence of a return statement in a function depends on whether the function is intended to produce an output (return a value) that can be used in other parts of the program.

The print_hello() function prints a string in the console, but we can't use this string in the program, manipulate it, or use it as an input for other functions. So, the print_hello() function has no return value.

In the next chapter, you will create your first function.

¿Todo estuvo claro?

Sección 1. Capítulo 1
course content

Contenido del Curso

Python Functions Tutorial

What Is Function?What Is Function?

In Python, a function is a named block of reusable code that performs a specific task. It is a fundamental concept in programming and plays a crucial role in organizing and structuring code. Functions are like building blocks in LEGO. They allow you to create reusable chunks of code that can be used in different parts of your program.

  • Firstly, it's convenient because you can call the same block of code multiple times without having to rewrite it. This saves time and reduces the chance of making mistakes;
  • Secondly, functions help break down complex tasks into smaller, understandable steps. Each function can perform a specific action, making the code more readable and easier to manage.

In simpler terms, functions are like breaking down tasks into smaller pieces that you can use over and over again. This approach makes your program more convenient to write, change, and understand.

Defining a function

Let's to define the most simple function in Python.

  1. Firstly, the def keyword has to be used;
  2. Secondly, we have to specify the function name - a unique identifier that represents the function. It follows variables' naming conventions, such as using lowercase letters and underscores ( this convention is called snake_case);
  3. We have to use (): after the function name - in these brackets, we specify the function's arguments (parameters);
  4. Then, we specify the function body with the indented new line;
  5. After the function body, we can specify the return value of the function using different keywords such as return, assign, or yield;
  6. Finally, we can use functions in code. To do so, we have to call function using its name and specifying its parameters.

Let's look at the example. We will create a simple function to print 'Hello, world!' in the console and use this function in code by calling it.

Code Description
You may have noticed that we used print() function as the body of print_hello(). It is a built-in function that prints the text in the console. It has a string as and arguments - we specify this string in the () brackets.

You may ask why this function has no return value if it prints the message in the console.
In Python, a function can perform a task (in this case, printing a message) without necessarily returning a value. The presence or absence of a return statement in a function depends on whether the function is intended to produce an output (return a value) that can be used in other parts of the program.

The print_hello() function prints a string in the console, but we can't use this string in the program, manipulate it, or use it as an input for other functions. So, the print_hello() function has no return value.

In the next chapter, you will create your first function.

¿Todo estuvo claro?

Sección 1. Capítulo 1
some-alt