Function definition in Python

towards-data-science

This post was originally published by Sadrach Pierre, Ph.D. at Towards Data Science

In computer programming, a function is a named section of a code that performs a specific task. This typically involves taking some input, manipulating the input and returning an output. In this post, we will go over how to define python functions, how to specify functions that can take an arbitrary number of positional arguments and how to specify functions that can take an arbitrary number of keyword arguments.

Let’s get started!

To start, let’s define a function that takes two arguments, in this case, two numbers, and returns the sum:

def sum_function(first, second):
return (first + second)

Let’s test our function with the number 5 and 10:

print("Sum: ", sum_function(5, 10))

This gives the expected result. We can also use the star expression in python to pass in an arbitrary number of arguments. For example, we can define a new function:

def sum_arbitrary_arguments(first, *rest):
return (first + sum(rest))

Let’s call this function with the number 5, 10 and 15:

print("Sum with arbitrary number of arguments: ", sum_arbitrary_arguments(5, 10,15))

We can call this function with any number of positional arguments. Let’s call the function with 5, 10, 15, 20, 25, and 30:

print("Sum with arbitrary number of arguments: ", sum_arbitrary_arguments(5, 10,15, 20, 25, 30))

This gives the correct value for the sum. We can also specify a function with default keyword arguments. For example, consider a function that by default prints the weather in NY:

def print_weather(name, state = "New York", temp = "30 degrees Celsius"):
print("Hi {}, it is {} in {}".format(name, temp, state))

If we call the function with “Sarah”:

print_weather("Sarah")

We can also overwrite the default values:

print_weather("Mike", "Boston", "28 degrees Celsius")

We can also write a function that takes an arbitrary number of keyword arguments using the ‘**’ expression. Let’s do this for weather reporting:

def print_weather_kwargs(**kwargs):
weather_report = ""
for argument in kwargs.values():
weather_report += argument
return weather_report

We then define a variable ‘kwargs’, for keyword arguments, as a dictionary and pass it into our function:

kwargs =  { 'temp':"30 degrees Celsius ", 'in':'in', 'State':" New York", 'when': ' today'}
print(print_weather_kwargs(**kwargs))

I’ll stop here but I encourage you to play around with the code yourself.

CONCLUSIONS

To summarize, in this post we discussed how to work with functions in python. First, we defined a simple function that allows us to find the sum of two numbers, then we showed that we can use the star expression to define a function that can take an arbitrary number of positional arguments. Next, we showed how to define a function with keyword arguments. Finally, we showed how to define a function that can take an arbitrary number of keyword arguments. I hope you found this post useful/interesting. The code from this post is available on GitHub. Thank you for reading!

Spread the word

This post was originally published by Sadrach Pierre, Ph.D. at Towards Data Science

Related posts