How to the Define Functions in Python to Save Space and Time!

by mottman1 in Circuits > Software

123 Views, 1 Favorites, 0 Comments

How to the Define Functions in Python to Save Space and Time!

Screen Shot 2022-11-16 at 11.41.33 PM.png

I made an instruction on how to use a function in Python which is called "Define" or "def." The reason why I made this is that when I started coding I quickly became unorganized with all of the new files I was creating every day, causing me to search for specific files for unnecessary amounts of time.  Learning how to define functions can help a person avoid repeating the same code over and over in a program. When you begin to deal with bigger and harder problems, being able to call many different functions will be a huge help.

Supplies

The only tools needed are a Python compiler and shell, which can be downloaded nearly anywhere on the internet, or you can simply use an online one that doesn't require downloading, both for free. Coding beginners, which is my audience, should already have one or the other prior to starting this instruction set, as they code without it.

Creating a File

Screen Shot 2022-11-16 at 11.09.12 PM.png

In order to use the Define or Def function, we must create a separate file to use it. Begin by creating a new file to create all of your functions. Do this by hovering over File which can be found in the top left corner, then by clicking on New File.

How to Use the Define Function(def)

Screen Shot 2022-11-16 at 11.13.11 PM.png

Your next step is to type out def, which stands for defined, then a space. You will then create a name for your function, which can be named anything. The parenthesis contains something called a parameter, which is usually required in writing code, but not always. You close the function by using a colon at the end.

Showing What to Do When Function Is Defined

Screen Shot 2022-11-16 at 11.17.58 PM.png

This is an example of a defined function with some code that does not have a parameter. On your own, create any lines of code under the defined function so you can run the function. To execute this function, in the same taskbar you found the file, there will be the option to run. Hover over run, then click on run module. It will require you to name and save the file. In my example, I got input from the user and want to print what they said.

Calling Your Defined Function

Screen Shot 2022-11-16 at 11.24.01 PM.png

Now all you have to do is call the name of your function. Type out the name of the function you created earlier with empty parenthesis if you didn't have parameters, or parenthesis with your parameters inside of them. In this example, you can see that when I called my function, the code I made in the file is executed.

Also, you are about halfway done with this instruction set! Keep it going!

What Happens When You Use a Parameter

Screen Shot 2022-11-16 at 11.47.13 PM.png
Screen Shot 2022-11-16 at 11.28.06 PM.png

This is an example of a function that has a parameter. A parameter is meant to interact with your code. In this example, the user will input their age, and the code we made will multiply their age by the parameter given. When I enter in the age, it automatically prints the parameter and age, which was the code we wrote. The green line of text under the defined function is called a doc string, to help whoever is interacting with your code understand what the purpose of the code is. The second picture shows what happens when you run this code.

You are almost done! You're doing a great job so far!

Using 'pass' in Defined Functions

Screen Shot 2022-11-16 at 11.35.06 PM.png

When making code, if it is not complete or has errors, no function in the file can be run. If for some reason you can't seem to figure out what the bug in your code is, you can use a pass so that you can continue your work and continue to be efficient. The pass will allows Python know you are still working on it and to ignore it so that there are no problems when running and executing.

Your Done!

Screen Shot 2022-11-16 at 11.38.52 PM.png

You now know how to define functions! Now you can save time, energy, and space by keeping your functions and codes organized in one file, or less than what you would usually use! As you progress as a coder, you will begin using functions within your functions, and by knowing how to define them, you will be able to do incredible work and create exceptional code. Great job learning this new skill, and I hope you learn many more on your coding journey!