Functions

--Originally published at Fundamentos de Programación

First of all we need to know what a function is.

krqbx2d_700wa_0

So a function is a block of reusable code that is used to perform an action.

Python already has some build in functions such as print(), len() and many others. But besides of being able to use the functions that Python already has we can create our own functions in a very simple way.

brkj4jm_700wa_0

I’ll make a function to transform anything you say to a shout.

First when defining a funtion is the keyword def followed by your funtion’s name and in “()” the input parameter that can also be left empty and finally don’t forget to put “:”.

The next thing to do is tell python what you want your function to do in an indented space. You can use conditionals, loops or whatever you want whithin the function.

You can make the function return anything with “return”, once any expresion is returned it will automatically stop running the rest of the code of the funtion so be carefull where you put a return statement.

shout func

To use a function you just need to write its name and put what it requires in the parenthesis like this:

call shout

This is the output of the function:

shout run

Mastery topics:

Calling functions

Creating Functions

Use of “if”

Validate user input (partially)

Source: http://www.tutorialspoint.com/python/python_functions.htm

#functions #python #tc101