Intro
This tutorial will teach you about functions in Python.
Requirements
- Know basics of Python 3 if not read here
- Knowledge of the procedural programming paradigm will help
What are they?
Functions are used to create reusable pieces code that can be run (called) at any time. They are essential to creating DRY code (don’t repeat yourself).
Calling (Run)
There are several ways to call a function. You first have to know the function name.
No Arguments
The first way is to call a function without arguments. We can do this using the function name and then putting brackets at the end.
|
|
You might recognise this name from earlier tutorials.
Arguments
We can also pass variables into a functions. You can do this by putting variables/values in between the brackets.
|
|
As you can see in the code above to pass multiple arguments into a function we use a comma. It also doesn’t matter whether it is a value being passed in or a variable.
Keyword Arguments
You can also pass in by keyword if the function has it implemented.
|
|
As you can see passing a keyword you place a equal sign before a keyword name. In this case “sep” is a keyword argument for the print function, it handles how each argument is separated when outputted.
When this code is run it will output: hello, world
.
Creating
Creating a function is very simple in Python.
No Arguments
First lets create a function that has no parameters. We first use the “def” keyword then the function name and then brackets that will have no content inside as it will take no parameters. Like a if statement everything that belongs to the function will be indented.
|
|
As you can see our function will just output hello world
.
Arguments
Now lets create a function that has two parameters. We need to have two variable names inside the brackets of the function, I am calling mine “a” and “b”. Notice I am using something new at the end, the “return” keyword will allow variables/values to be passed outside of the function; which allows for them to be captured in another variable outside of the functions scope.
|
|
This function should return 8. The code will also output the 8
.
Extra Arguments
Now I will show you how Python will allow us to accept multiple parameters. We do this using the “*args” keyword.
|
|
The name after the “*” can be anything as it is just a variable name “args” is just a preferred unofficial standard.
Using this function we can see that we can pass any number of arguments to it. We can also pass nothing to it.
When run this code will output:
|
|
The data-structure of the variable is actually a tuple (similar to a list).
Keyword Arguments
Lastly we can use two “**” to allow passing through values by keywords.
|
|
This will output the keywords and the values. Content: {'one': 1, 'two': 2, 'three': 3}
.
The variable kwargs is a dict data-structure.
The **kw should always be the last argument.
Extra Info
We can use a mixture of arguments, keyword arguments for functions. Here are some examples:
|
|
We can also use the “*” and “**” as parameters when calling the functions. For example we an create a list and then pass it in as separate variables into the “*args”.
|
|
Area Of Circle
Lets put this knowledge to use. You are going to code a function that will calculate the area of a circle.
We will be assuming that pi is equal to 3.14159. If you don’t know how to calculate the area of a circle you can probably find a tutorial online.
- capture radius input
- convert to float
- create a function that takes in radius
- pi is equal to 3.14159
- return the area from function
- output the area returned
Try to code your own version before looking at the completed code below.
|
|
Lets test our application with the number 5. This should output 78.53975
. If yours outputs that, well done, if it doesn’t take a look at my code to see where you went wrong.
Research
You know some of the built-in functions already, however you should probably know some more as they all have useful features. To find more you can find them at Python’s documentation here.