Enchanted Code

Python 3 functions

5 minutes read

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.

1
print()

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.

1
2
3
name = "Leo"
print("hello")
print("hello", name)

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.

1
print("hello", "world", sep=", ")

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.

1
2
3
4
def hello():
    print("hello world")

hello()

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.

1
2
3
4
5
6
def multiply(a, b):
    c = a * b
    return c

answer = multiply(2, 4)
print(answer)

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.

1
2
3
4
5
def lots_of_params(*args):
    print("Content:", args)

lots_of_params(1, 2, 3, 4)
lots_of_params()

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:

1
2
Content: (1, 2, 3, 4)
Content:

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.

1
2
3
4
def lots_of_params(**kwargs):
    print("Content:", kwargs)

lots_of_params(one=1, two=2, three=3)

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:

1
2
3
4
a, b, *args
a, b, **kw
a, b, *args, **kw
*args, **kw

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”.

1
2
3
4
5
6
def lots_of_params(*args):
    print("Content:", args)

the_params = [1, 2, 3, 4]

lots_of_params(the_params)

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.

  1. capture radius input
  2. convert to float
  3. create a function that takes in radius
  4. pi is equal to 3.14159
  5. return the area from function
  6. output the area returned

Try to code your own version before looking at the completed code below.

1
2
3
4
5
6
7
8
def calculate_area_of_circle(radius):
    pi = 3.14159
    area = pi * radius**2
    return area

radius = float(input("Radius: "))
the_area = calculate_area_of_circle(radius)
print(the_area)

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.

See Also

Buy Me A Coffee