Apuntes
functions in python
Contenido del apunte (2)
Página 1
"functions in python"
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating a Function:
-------------------------------
In Python a function is defined using the def keyword:
Example:
def my_function():
print("Hello from a function")
Calling a Function:
----------------------------
example:
def my_function():
print("Hello from a function")
my_function()
output:
Hello from a function
Parameters:
-----------------
Information can be passed to functions as parameter.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a function with one parameter (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
output:
-----------
Emil Refsnes
Tobias Refsnes
Linus Refsnes
Página 2
Información
- Descripción
- Apunte sobre functions in python, creado por Sandeep Sunny el 05/08/2019.
- Creado por
- Sandeep Sunny · ago 2019
- Actualizado
- 5 ago 2019
- Visitas
- 4
- Etiquetas
- Valoración