Defining Your First Function | Functions
Introduction to Python

In earlier examples, like those in the "Conditional Statements" section, we often assigned different values to a variable just to verify our code was functioning correctly. This method can be cumbersome, especially as our code grows more extensive. A more efficient approach involves using functions. Here's the general syntax for defining a function:

Note

All lines of code within the function's body must have consistent indentation, whether it's 2 spaces, 3, 4, or another value.

For instance, let's craft a basic function that takes in two numbers (we'll call them `a` and `b`) and returns the square of their sum.

Success! Indeed, `2 + 3` equals `5`, and `5` squared is `25`.

¿Todo estuvo claro?

Sección 6. Capítulo 3

Contenido del Curso

Introduction to Python

In earlier examples, like those in the "Conditional Statements" section, we often assigned different values to a variable just to verify our code was functioning correctly. This method can be cumbersome, especially as our code grows more extensive. A more efficient approach involves using functions. Here's the general syntax for defining a function:

Note

All lines of code within the function's body must have consistent indentation, whether it's 2 spaces, 3, 4, or another value.

For instance, let's craft a basic function that takes in two numbers (we'll call them `a` and `b`) and returns the square of their sum.

Success! Indeed, `2 + 3` equals `5`, and `5` squared is `25`.

¿Todo estuvo claro?

Sección 6. Capítulo 3