Getting started with Functions in Python

Ayelegun Michael Kayode
Backticks & Tildes
Published in
5 min readMay 1, 2020
Source: 123RF

Most applications, regardless of the programming language they are written in, are usually broken down into smaller blocks of code known as functions. In this tutorial, we are going to take a look at how to work with functions in Python. So fire up your favorite text editor (hopefully it’s VS Code 🤞) and let’s get started.

Note: You need to have Python (preferably Python 3 as that is the future of the language) installed on your local machine to follow along. If you don’t have Python installed on your machine, head over to the python download website and download the latest version. At the time of writing, the latest version is 3.8.2, and support for Python 2 already stopped.

Basic Functions

If you have Python installed on your machine, go ahead and create a new file called functions.py. Paste the following line in:

def my_first_function():
print("I am a function")

To run this code, type python3 function.py

Let’s break down what’s happening here:

  • We define a function in Python using the def keyword. Keywords in programming are a set of reserved words that connote a particular action.
  • Next, we give our function a name. Here, we are calling it my_first_function. The parentheses in front of our function name tell us this is a function to be executed. Right next to it is a colon. The colon here is used to create a scope for the function. Everything after the colon is scoped to the function, meaning whatever variables are created after the colon is only available within the function.
  • On the next line, we simply print out “I am a function” to the console.

Now, let’s add a couple more lines of code:

Let’s explain what we’ve got here:

  • On line 4, we execute the function which is just a fancy way of saying the function got executed. By executing the function, we run every line of code defined within that function. One thing to note is that print is a function, and functions can call other functions, including themselves. Here, it’s simply a call to print(“I am a function”). That causes the string “I am a function” to get printed to the console.
  • On line 5, we call the print function and pass to it a call to the func1 function. What happens here is func1 runs and prints “I am a function” to the console, and then the return value of calling func1 gets logged to the console. Since we return nothing from this function, None gets logged to the console.
  • On line 6, we simply print a reference to the func1 function. It doesn’t get called, so what gets logged to the console is a reference to our func1 function.

Functions that take arguments

Functions become more powerful when we require them to do more complicated stuff, like take some inputs and perform calculations on them. Let’s look at the example below:

Here, we are defining a function called addNumbers using the def keyword, and we define two parameters within its parenthesis: arg1 and arg2. On the next line, we simply print the result of adding arg. So when we specify the integers 4 and 5 as arguments to be used by the function by adding them to the parenthesis during the execution, the function receives the integers and adds them, after which the result of the addition is printed to the terminal.

A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method’s parameters. Parameter is variable in the declaration of function. Argument is the actual value of this variable that gets passed to function

Functions that return a value

Another useful thing we can do with functions is return values after executing our function. This is crucial because sometimes we need to save the result of our function execution in a variable to be used somewhere else. Take a look at the snippet below:

Let’s walk through what’s happening here:

  • We define a function called cube, that takes an argument. Then within the body of the function, we return num multiplied by itself thrice. (Basically, num raised to the power of 3).
  • Then on line 4, we print the sum of 3 and the result returned from passing 3 to cube. Because our function returns a value, that is the same as doing print(3+27). Functions can be very powerful when used this way.

Functions with a default value for an argument

Extending the above functionality, functions can have default values set as an argument. So when we don’t pass anything to the function, the function makes use of the default value whenever it is executed. Take a look at the example below:

  • We define a function called power with two arguments: num and x. Notice how x is set to a default value of 1.
  • Next, we set result to equal 1 and run a for-loop. This loop runs for the number of times equal to x (so if x is 1, the loop runs once, if x is 2, the loop runs twice. You get the picture now).
  • Within the body of the function, we multiply result by num and set that to be the new value of result. Then we return the result. So for a scenario where x is 2, like in line 8, result starts off as 1. then on line 4, we multiply result by num (4, in this case). Then the loop runs again. This time, result is 4, and we multiply it by num (which is 4) to get 16. The loop ends and we return 16.

Functions with a variable number of arguments

Sometimes, we don’t know beforehand how many arguments our function would end up taking. So we can’t define a fixed number of arguments. To get over this hurdle, python provides a handy syntax: *args. This syntax allows us to pass a variable number of arguments to a function. Take a look at the snippet below:

  • We define our function called multi_add, and pass it the *args syntax. By using this syntax, we turn our arguments into a list containing all the arguments we passed to our function.
  • We set result = 0, and loop through every element in *args.
  • Within the loop, we add result to each element in the *args list and return the result.

And there we have it. A basic introduction to functions. Hopefully, you can build on this new-found knowledge.

--

--

Ayelegun Michael Kayode
Backticks & Tildes

Software engineer, 10 years exp. Tech enthusiast, lifelong learner, leader. Sharing insights on coding, leadership & tech philosophy.