Hello everybody,
Michael here, and today’s post will be a Python post on functions.
What is a Python function? Let me explain this concept using a Java method:
public int multiply (int e, int f)
{
return e*f;
}
Methods in Java are blocks of code that run only when they are called. Data can be passed into a function in the form of parameters. Methods can return data as a result (unless the method is void, in which case there is no return type).
So what would this have to do with functions? Well, Java methods and Python functions behave the same way (with some differences); Python functions can do everything that Java methods can and behave similarly to Java methods. Python functions only run when they are called. You can pass data into Python functions in the form of parameters. Python functions can also return data as a result.
Now let’s create a simple function, which calculates someone’s BMI based on their height and weight:

First thing to remember is that all functions in Python start with def. It doesn’t matter what type of value the function is returning or the value type of the parameters-all functions in Python start with def. To call the function, simply use the function name followed by parentheses.
Also, all Python functions start with a colon and always have indented bodies; Java methods, on the other hand, start and end with curly brackets and don’t always have indented bodies.
Another thing you probably noticed is that the two parameters don’t have the value type mentioned; in other words, you just see weight and height-not int weight or int height. This is because, unlike in Java, mentioning value types isn’t necessary in Python functions.
The fact that you don’t need to mention a value type in the function parameters also allows for more flexibility with your function. For instance, I could pass either int or float values in the BMI function and the function would work fine; however, if you try to pass a non-numerical value into the parameters, you’d likely get an error.
In this example, I used 166 and 70 as parameters (which represent the weight and height (in inches), respectively). The result is a BMI of 23.8 (rounded to one decimal place).
Another thing to keep in mind with Python functions is that if there are a certain number of parameters passed into a function, then when you call that function, you must use the same number of parameters-no more, no less. For example, the BMI function takes two parameters-weight and height-so if you try more or less than two parameters into the function when you call it, you will get an error.
Well, that holds true for the most part. See, with Python functions, you must remember to use the correct number of parameters when you call the function. But there is a workaround to this rule that you can’t find in Java-its the *args functionality.
*args is shorthand for arbitrary arguments*, and you would use this functionality if you don’t know how many arguments your function will use. Here’s an example of *args in action:
*The terms arguments and parameters are used interchangeably in the context of Python functions; you’ll see me using these terms interchangeably as well.

To use the *args functionality, simply place an asterisk right next to the variable that you want to use as a parameter. In this example, I want to use the *args functionality for the cities variable.
So for functions with the *args functionality, the parameter would be a tuple rather than a single value. However, for the function to know which value in the tuple to return, remember to specify the index by the variable name; in this case, I used cities[3] to tell the function to return the 4th value in the tuple (whatever that might be).
- A good suggestion to keep in mind when using the
*argsfunctionality is that, when you want to select a particular element from the tuple, be sure that the index exists in the tuple. For instance, if I had selectedcities[5]from the tuple in the example, I would’ve gotten an error since there was no index 5 in the tuple.
*args isn’t the only cool functionality when it comes to Python functions. There is another functionality called *kwargs (short for keyword arguments) which allows you to send arguments to the function as a list of keys and values (like you would have in a dictionary). Let’s demonstrate the *kwargs functionality:

In this example, the parameters for the teams function are specified as pairs of keys (team1, team2, and team3) and values (Philadelphia 76ers, Miami Heat, and LA Lakers). The key team2 is specified in the print line of the function; this tells the function to return the value that corresponds to team2, which is Miami Heat.
But what happens if you don’t specify a certain number of key-value pairs for the parameters? You can use the **kwargs functionality (which is shorthand for arbitrary keyword arguments), which is like the *kwargs functionality except you don’t need to specify how many key-value pairs the function will use (also, the **kwargs functionality uses two asterisks right by the variable name in the parameter, but this seemed pretty obvious).
Let’s take a look at the **kwargs functionality in action:

In this example, the function firstName receives a dictionary of arguments and can access the items according to the key called in the print line-in this case, the key name1 is being called, so the corresponding value that will be printed is Michael.
- In order to indicate the value that you would like to return, call the corresponding key in the function. If you try to call the value, you’ll get an error.
OK, so I’ve discussed parameter values and arbitrary parameter values. But what happens when we have a default parameter value? Let’s take a look:

In this example, I set the default parameter value of age to 24. I then called the function twice, first with empty parentheses, then with a parameter of 22. When I called the function with empty parentheses, 24 was returned as the value for age. However, when I used 22 as a parameter, the default value for age was over-ridden and 22 was instead returned as the value for age.
- You likely noticed that I have 24 and 22 as
String, notint. This is becauseStringvariables can only be concatenated with otherStringvariables.
By now, you have a better idea as to how to pass individual values as parameters in Python functions. However, you can also pass any other type of value as a parameter in a Python function (whether it’s a list, tuple, dictionary, etc.). As a matter of fact, I already demonstrated passing a dictionary as a parameter when discussing the *args and **kwargs functionalities. Now let’s demonstrate passing a list as a parameter:

In this example, I pass the list days as a parameter in the function listFunction. I also make sure to include a for loop to iterate through the list.
- In this example, there is only supposed to be one parameter for this function, which means you’re only supposed to pass one list through the function. However, you can have an unlimited number of values in the list.
Next, here’s an example of how functions can return values with the return statement:

In this example, the function division is returning the value of the parameter divided by 5. The way to call a function with a return statement is different than the way to call a function without a return statement; you need to place the function call-division()-inside a print statement.
Now the last thing I want to mention regarding Python functions is that, unlike Java methods, they can’t be empty. Well, there is a workaround….:
![]()
To create a function with no content, simply use the keyword pass in the body of the function, and you’ll be able to create it without running into an error (and yes, I know the name emptyFunction is oxymoronic as functions can’t be empty).
Thanks for reading,
Michael