Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy 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 fromall 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 deletionsexercises/05-Defining-vs-Calling-a-function/README.es.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
# `05`Definir vsllamar auna función
# `05`Defining vsCalling aFunction

Las funciones solo existen si tú u otra persona las define... es la única forma en que el compilador/intérprete deidiomas sabe que existen, por lo tanto, puede ejecutarlas cuando lasllama.
Las funciones solo existen si tú u otra persona las define... es la única forma en que el compilador/intérprete delenguaje sabe que existen, por lo tanto, puede ejecutarlas cuando lasllamas.

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

```python
defmyFunctionName(parameter, parameter2, ...parameterX):
#codigo de la función aquí
returnsomething
defnombre_de_funcion(parametro1, parametro2, ...parametroX):
#Código de la función aquí
returnalgo
```

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 deletionsexercises/05-Defining-vs-Calling-a-function/README.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,57 +2,47 @@
tutorial: "https://www.youtube.com/watch?v=fz4ttmwZWuc"
---

# `05` Defining vs Calling afunction
# `05` Defining vs Calling aFunction

Functions will onlyexists 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 onlyexist 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
defmyFunctionName(parameter, parameter2, ...parameterX):
#the function code here
defmy_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 deletionsexercises/05-Defining-vs-Calling-a-function/app.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
# Define the function called "multi" that expects 2 parameters:
# Definebelowthe 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 deletionsexercises/05-Defining-vs-Calling-a-function/solution.hide.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
# Define the function called "multi" that expects 2 parameters:
def multi(num1, num2):
# Definebelowthe 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 deletionsexercises/05-Defining-vs-Calling-a-function/tests.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
import io, sys, pytest, os, re, mock

@pytest.mark.it("Createa function 'multi'")
@pytest.mark.it("Createthe 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

[8]ページ先頭

©2009-2025 Movatter.jp