How to Make Grapefruit Your Brunch Hero

Grapefruit is a classic for brunch but can feel somewhat uninspiring in its traditional form — halved and topped with brown sugar. It has so much more to offer a brunch crowd. With a few tweaks in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Make a Function in Python

This blog post will walk through the steps on how to make a function with Python.

Why Would We Want to Make a Function?

If we find ourselves repeating the same steps multiple times during a workflow, that is a good indicator to let us know it’s time to outsource these steps to a function. If we are exploring some data in a new data set, often times we will find ourselves repeating the same steps multiple times. One such example may include wanting to use a specific plotting technique multiple times on different areas of our data. Instead of copy/pasting the code over and over, or god forbid retyping the plotting code each time we want to use it, we can generalize the plotting technique and create a function that we can easily call whenever we need.

Step One - Define the Function

Anytime we want to make a function, we always need to start with the code on line 3. This ‘def’ stands for ‘define,’ thus we need to start by defining our function. After the space, we create the name of our function. For the example above, I simply named the function ‘func.’ Once we’ve set the name of our function, we must always include a set of open and close parentheses followed by a colon. We will almost always need to include parameters inside these parentheses when defining functions, but I will come back to that in a future step.

Step Two - Don’t Forget the Return Statement

Next, we want to include our return statement. To do this, we could manually move to a new line and create an indentation of four spaces, however, most coding environments will automatically do this step for us once we press the return key when our cursor is to the right of the colon. Any code we want included in our function should always be tabbed over at least this many times, otherwise Python will not understand that it is part of the function.

The return statement will tell our function what to output once we call and run it. For this simple example, I have told the function to return the string ‘Hello, World’ on line 4. Once we run this code, there will be no output, but the function will be saved and we can call it any time we want in the future. The image below shows an example of how to call the function we just created as well as what the output should look like.

Output When the Function ‘func()’ is Called

Next I Will Walk Through the Steps to Make a Function that Handles Parameters

While the function that was just created is great for pedagogical reasons, it doesn’t really have any meaningful use in the real world. Below I will create a function that potentially has slightly more value: an exponent calculator.

Step One - Define the Function on the First Line, Include Parameters

Now we will walk through a function that will take in 2 arguments, x and n, and return the result after raising x to the n’th degree. The code on line 5 is very similar to the code in the ‘func()’ function that we walked through previously - the only differences are the name of the function and the fact that there are parameters here - everything else on the first line is exactly the same. I named this function ‘power’ because its purpose is to perform an exponential operation and return the result; there is an art to naming functions, but it is important that a function’s name is concise and relevant to its use.

Step Two - Perform Operations

On line 6, the parameters x and n are used to create the result that this function will return. We simply set ‘result’ equal to the operation. We could do a lot of things with x and n here, such as find their sum, quotient, or difference, as long as we: 1) used different variable names for each of those operations, and 2) included these operations on lines above the line that our return statement is on.

Step Three - Return the Result, Test Function

The final step in creating this function is simple, on line 7 we simply return the result variable that we set equal to our exponential operation. Once we complete the creation of the function, we can test it by calling it and adding any integers of our choice as arguments - I will use a few examples to test this. If we did everything correctly, the call and output should look like this:

Add a comment

Related posts:

How to Start a Business That Constantly Gets You Money

When you first start a business, it can feel overwhelming. You have all the necessary resources, and you’re trying to figure out how to get started. But don’t worry — there are a few basic steps you…

10 Traits of Great PMs

PMs have diverse backgrounds, murky responsibilities, and wildly varied expectations across companies. It’s nearly impossible to define what makes a PM great. With those caveats, here is an attempt…

Jab Imtiaz Wrote JHMS

After films like Love Aaj Kal, Tamasha or Jab We Met, Imtiaz Ali should ideally have improved in his story telling. Neither did that happen nor did he get a good scriptwriter. The film takes you…