-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ignacio Avas
committed
May 3, 2017
1 parent
0f42915
commit 2fc5121
Showing
5 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,52 @@ | ||
Boilerplate | ||
----------- | ||
Funciones | ||
--------- | ||
|
||
Insertar alguna introducción aquí. | ||
Una función es un bloque de código organizado y reusable que puede ser utilizado para realizar cierta acción. Python ofrece un número importante de funciones predefinidas como print() o len(), pero se pueden definir nuevas funciones en el código, que son denominadas funciones definidas por el usuario. | ||
|
||
.. sourcecode:: python | ||
|
||
print('Some python code here') | ||
def di_hola(): | ||
print("Hola") | ||
print("¿Cómo estás?") | ||
|
||
Las funciones también pueden recibir argumentos: variables que se pasan desde el invocador a la función. Por ejemplo | ||
|
||
.. sourcecode:: python | ||
|
||
def decir_hola_con_nombre(name): | ||
print("Hola {{0}}".format(name)) | ||
print("¿Cómo estás?") | ||
|
||
Las funciones también pueden retornar un valor al invocador, usando la palabra clave "return", por ejemplo: | ||
|
||
.. sourcecode:: python | ||
|
||
def hacer_division(x, y): | ||
if y != 0: | ||
return x / y | ||
else: | ||
print("y no puede ser 0") | ||
return 0 | ||
|
||
En algunos casos las funciones pueden ser vacías y no hacer nada. Por ejemplo si se planea ser escritas luego. En esos casos, esas funciones solo tienen la palabra clave "pass" en el cuerpo | ||
|
||
.. sourcecode:: python | ||
|
||
def a_ser_escrita_luego(lista_de_ingredientes): | ||
pass | ||
|
||
|
||
Llamando a una función | ||
---------------------- | ||
Definir una función solo le da un nombre, especifica los parámetros que se van a incluir en la función y estructura los bloques de código. Una vez que la estructura de una función está terminada, puede ser ejecutada llamándola. En el siguiente ejemplo se llaman a las funciones que fueron definidas previamente: | ||
|
||
.. sourcecode:: python | ||
|
||
result = hacer_division(2343, 22) | ||
decir_hola_con_nombre("Alex") | ||
print("Resultado es {{0}}".format(result)) | ||
|
||
Desafío | ||
------- | ||
--------- | ||
|
||
Describir el desafío aquí. | ||
Crea un programa que imprima los números pares desde el 2 al 100 inclusive, definiendo al menos una función y llamándola. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,54 @@ | ||
Boilerplate | ||
----------- | ||
Functions | ||
--------- | ||
|
||
A function is a block of organized, reusable code that is used to perform a single, related action. Python already define many built-in functions like print() or len(), but new functions can be defined in the code, which are called user-defined functions. | ||
|
||
.. sourcecode:: python | ||
|
||
def say_hello(): | ||
print("Hello") | ||
print("How are you?") | ||
|
||
Insert some introduction here. | ||
Functions may also receive arguments (variables passed from the caller to the function). For example: | ||
|
||
.. sourcecode:: python | ||
|
||
print('Some python code here') | ||
def say_hello_with_name(name): | ||
print("Hello {{0}}".format(name)) | ||
print("How are you?") | ||
|
||
Also they may return a value to the caller, using "return" keyword. For example: | ||
|
||
.. sourcecode:: python | ||
|
||
def make_division(x, y): | ||
if y != 0: | ||
return x / y | ||
else: | ||
print("y can't be zero") | ||
return 0 | ||
|
||
In rare cases some functions may be empty and do nothing. For example if they are planned to be written later. In those cases, those functions have only the "pass" keyword on the body: | ||
|
||
.. sourcecode:: python | ||
|
||
def will_be_written_tomorrow(list_of_ingredients): | ||
pass | ||
|
||
|
||
Calling a Function | ||
------------------ | ||
|
||
Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code. Once the basic structure of a function is finalized, it can be executed by calling it. Following is the example to call the previous defined functions: | ||
|
||
.. sourcecode:: python | ||
|
||
result = make_division(2343, 22) | ||
say_hello_with_name("Alex") | ||
print("Result is {{0}}".format(result)) | ||
|
||
Challenge | ||
--------- | ||
|
||
Describe the challenge here | ||
Create a program that prints the even numbers from 2 through 100 defining at least a function and calling it. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
.. sourcecode:: python | ||
|
||
print('Some solution here') | ||
def print_even(upper_bound): | ||
for number in range(upper_bound+1): | ||
if number % 2 == 0 and number > 1: | ||
print(number) | ||
|
||
print_even(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters