Learn Python For Beginners

Functions In Python

Functions in python make it easier to write reusable code. Code reuse is a pillar of efficient software, python or not. We have already seen and used many functions in this series of lessons for learning python for beginners. The bigger a program grows the harder it becomes to maintain. Remember the DRY principle as we continue this lesson (and then for the rest of your life). DRY stands for Don’t Repeat Yourself. Functions help us do that.

Writing Functions In Python

Python only has so many functions built-in. If we want to write efficient code, we are going to have to write our own functions. If you find yourself writing the same logic more than one time, make it a function! As Long as you follow that rule you are mitigating the risk of repeating yourself. Let’s see a simple to understand a scenario where this makes more sense.

Notice we are running the logic to get a value more than once. Even though the program is running as expected, but the larger the software grows it will become harder to read if we have to write the logic every time we need it. This is where writing your own functions in python comes in. We will put this same logic in a function and use it as many times as we need.

Functions In Python

Writing our own functions makes our software easier to read and it acts the same way as before. See the a and b variables? These are called parameters. Parameters are values a function needs from the space you are calling it to run the nested logic. You can write functions that don’t require any parameters by leaving the parentheses blank, but we need them here. If you look closely, what happened when we ran the function was we took whatever value we passed in the a parameter, added it to the value in the b parameter, then ran the rest of the logic. The term parameter and arguments are interchangeable if you see them referred to in that way.

Still, we can make this logic even more efficient. We can actually store the solution to our operation inside the function to another variable. This is known as returning from functions in python.

Returning From Functions

Returning values from a function and storing them in memory with a variable is a common occurrence in any programming language. Python is not different. Let’s take the lessons learned from python if statements and combine it with this new idea.

Functions In Python

The logic of the function is simple. Two numbers are compared and the larger number is returned. Instead of just calling the method like before, we are declaring new variables and assigning the returned value of the function to that variable. Be aware that once a function reaches a return statement it stops executing. Any logic that exists after a return statement would not run once a value has been returned. You are able to return nothing as well. This may be necessary to exit a loop in a function in unwanted scenarios. If we are only running a function on numbers and we receive a string, we would return nothing.

More On Functions In Python

Now that you understand the basics of writing custom python functions, I urge you to take some time and experiment. Try writing a program with a function that takes a string from user input as a parameter as well as a list of other strings and adds the input to the list and returns the value of the new list. Doing things like this in your free time will only increase your skill set. We will be returning to functions in python later as they have a different application in object-oriented programming in python.

Leave a Comment

Your email address will not be published. Required fields are marked *