Skip to content

exercise 05-defining-vs-calling-a-function #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions exercises/05-Defining-vs-Calling-a-function/README.es.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# `05` Definir vs llamar a una función
# `05` Defining vs Calling a Function

Las funciones solo existen si tú u otra persona las define... es la única forma en que el compilador/intérprete de idiomas sabe que existen, por lo tanto, puede ejecutarlas cuando las llama.
Las funciones solo existen si tú u otra persona las define... es la única forma en que el compilador/intérprete de lenguaje sabe que existen, por lo tanto, puede ejecutarlas cuando las llamas.

Para definir una función necesitamos escribir esta fórmula básica de código:

```python
def myFunctionName(parameter, parameter2, ...parameterX):
# codigo de la función aquí
return something
def nombre_de_funcion(parametro1, parametro2, ...parametroX):
# Código de la función aquí
return algo
```

La palabra `def` es una palabra reservada en Python, esto significa que solo se usa para definir una función.
Expand All @@ -16,13 +16,11 @@ La palabra `def` es una palabra reservada en Python, esto significa que solo se

*Consejo:* usa un nombre descriptivo (no intentes ahorrar palabras, usa tantas como necesites) de esta manera entenderás lo que hace la función (y lo que devuelve).

Nombres de ejemplo: `add_two_integers` (suma dos números enteros), `calculate_taxes` (calcular impuestos) , `get_random_number` (obtener número aleatorio), etc.
Nombres de ejemplo: `add_two_integers` (suma dos números enteros), `calculate_taxes` (calcular impuestos), `get_random_number` (obtener número aleatorio), etc.

**Parámetros:** puedes definir tantos parámetros como desees, más aún, si los necesitas.
**Parámetros:** puedes definir tantos parámetros como desees, más aún, si los necesitas. La cantidad de parámetros dependerá de las operaciones realizadas dentro de la función.

La cantidad de parámetros dependerá de las operaciones realizadas dentro de la función.

Ejemplo: si la función está sumando dos enteros (3 + 4), esto significa que la función necesitará dos parámetros (uno para cada entero).
Ejemplo: si la función está sumando dos enteros (a + b), esto significa que la función necesitará dos parámetros (uno para cada entero).

**Alcance:** Todo el código que contenga la función debe tener una sangría a la derecha, todo lo que esté en una sangría diferente no será considerado como parte de la función, a esto se llama **alcance**, y puede ser local (dentro de la función) y global (fuera de la función).

Expand All @@ -34,7 +32,7 @@ Ejemplo de una función:

```python
def concatenate_number_to_string(local_number, local_string):
local_variable = local_string+""+str(local_number)
local_variable = local_string + str(local_number)
return local_variable
```

Expand Down
40 changes: 15 additions & 25 deletions exercises/05-Defining-vs-Calling-a-function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,47 @@
tutorial: "https://www.youtube.com/watch?v=fz4ttmwZWuc"
---

# `05` Defining vs Calling a function
# `05` Defining vs Calling a Function

Functions will only exists if you or somebody else defines them... it is the only way the language compiler/interpreter knows they exist, therefore it's able to run them when you call them.
Functions will only exist if you or somebody else defines them; it is the only way the language compiler/interpreter knows they exist, therefore it's able to run them when you call them.

To define a function we need to write this basic code formula:
To define a function, we need to write this basic code formula:

```python
def myFunctionName(parameter, parameter2, ...parameterX):
# the function code here
def my_function_name(parameter1, parameter2, ...parameterX):
# The function code here
return something
```

The word `def` is a reserved word in Python, this means it is only used to define a function.

**The name** of the function could be anything you like.
Tip: use a descriptive name (don't be cheap with words,
use as many as you need) this way you will understand what the function
does -and returns-.
Example names: add_two_integers , calculate_taxes , get_random_number, etc.
**The name** of the function could be anything you like. Tip: Use a descriptive name (don't be cheap with words, use as many as you need); this way, you will understand what the function does -and returns-.

**Parameters:** you can define as many parameters as you like or need.
The amount of parameters will depend on the operations done inside the function,
I.E: if the function is adding two integers `(3 + 4)` this means the function
will need two parameters (one for each integer).
Example names: `add_two_integers`, `calculate_taxes`, `get_random_number`, etc.

**Scope:** All the code that the function will contain need to be indented
one tab to the right, anything on a different indentation
won't be considered as part of the function,
this is called **the scope**, and it could be local (inside the function)
and global (outside of the function).
**Parameters:** You can define as many parameters as you like or need. The amount of parameters will depend on the operations done inside the function. I.E: If the function is adding two integers `(a + b)` this means the function will need two parameters (one for each integer).

**The Return**: not every function needs to return something, but it is recommended that it does.
Tip: returning `None` is a good default for when you, still, don't know if you need to return something.
**Scope:** All the code that the function will contain needs to be indented one tab to the right, anything on a different indentation won't be considered as part of the function. This is called **the scope**, and it could be local (inside the function) and global (outside the function).

**The Return**: not every function needs to return something, but it is recommended that it does. Tip: returning `None` is a good default for when you still don't know if you need to return something.

Example of a function:

```python
def concatenate_number_to_string(local_number, local_string):
local_variable = local_string+""+str(local_number)
local_variable = local_string + str(local_number)
return local_variable
```


# 📝 Instructions:
## 📝 Instructions:

1. Define a function called `multi`.

2. The `multi` function receives two numbers.

3. Return the result of the multiplication between them.

## 💡 Hint
## 💡 Hint:

+ Remember to add the `return` line. Every function should return something, in this case it should be the result of the multiplication.
+ Remember to add the `return` line. Every function should return something, in this case, it should be the result of the multiplication.
6 changes: 3 additions & 3 deletions exercises/05-Defining-vs-Calling-a-function/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Define the function called "multi" that expects 2 parameters:
# Define below the function called "multi" that expects 2 parameters

# don't edit anything below this line
# Don't edit anything below this line
return_value = multi(7,53812212)
print(return_value)
print(return_value)
10 changes: 4 additions & 6 deletions exercises/05-Defining-vs-Calling-a-function/solution.hide.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Define the function called "multi" that expects 2 parameters:
def multi(num1 , num2):
# Define below the function called "multi" that expects 2 parameters
def multi(num1, num2):
total = num1 * num2
return total


# don't edit anything below this line
# Don't edit anything below this line
return_value = multi(7,53812212)
print(return_value)
print(return_value)
6 changes: 3 additions & 3 deletions exercises/05-Defining-vs-Calling-a-function/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io, sys, pytest, os, re, mock

@pytest.mark.it("Create a function 'multi'")
@pytest.mark.it("Create the function 'multi'")
def test_declare_variable():
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
with open(path, 'r') as content_file:
Expand All @@ -20,6 +20,6 @@ def test_for_return_something(capsys, app):
def test_for_integer(capsys, app):
assert app.multi(3,4) == 12

@pytest.mark.it('The function multi must receive two numbers and return their multiplication. Testing with different values.')
@pytest.mark.it('The function multi must receive two numbers and return their multiplication. Testing with different values')
def test_for_function_return(capsys, app):
assert app.multi(10, 6) == 60
assert app.multi(10, 6) == 60