LET’S MAKE COOKIES! An intro to Functions

--Originally published at Coding The Future

Photo by Jade Wulfraat

When I think of functions in any programming languages, I think of them as the reusable tools you use when baking cookies, for example. Here's why...

Every time you make cookies, you don't buy a new bowl, a new mixer, a new oven, and a new tray, or do you? If you do, you're just... something else. However, must of us reuse the same materials. There's obviously the ingredients that are single-use only, but most of the stuff can be reused and that is a good thing, because that way there's no need to buy a new tray every time we want to bake cookies.

Translating this into programming, if we are going to be using the same code several times, there's no point in writing it several times. It only makes sense to write it once, and use it (or call it as many times as we need to.

So basically, a function is "a block of organized, reusable code that is used to perform a single, related action" 1 that improves the efficiency and speed of your code. Remember that less lines of code means also better readability.

Defining a Function

When defining a function, start by typing the keyword def (I'm guessing it's short for define), followed by a unique name for the function, and then include brackets where any input parameters will also be declared (think of input parameters as the single-use ingredients that can change depending on what you want to achieve). Close the line with a colon.

In the following lines, which are always indented, is where the magic happens. You can include a explanation what the function does in the first line in quotation marks, although this is optional. This will not affect your function, and is often referred as docstring. All operations and variables must be written underneath the docstring, but be mindful that indentation counts.

Lastly, to end off a function, type return and specify what the function will literally return to where it was called. Most times, functions return a variable. If you don't want to return anything, just leave the return line blank.

Here's an example of a simple function:

def newFun (num1, num2): "Takes two numbers and adds them" num = num1 + num2 return num

As you can see, in the first line, the function is defined and given the name newFun, followed by two parameter variables, which are required if we want to use this function. In the second line, the doctoring describes what the function does. In the third line, a new variable called num is declared, and the sum of the two parameters that were passes is stored in it. Lastly, the third line returns the value of num.

Calling Functions

Let's say that while writing some code, I suddenly need to use the function I declared above. To call it, all I would need to do is to type the name of the function, followed by brackets, where I would specify which values or variables I will pass as parameters. If the function is defined with a certain amount of parameters, it is mandatory that the required variables are included in the brackets.

Here's an example:

var1 = int(input('Enter a number: '))
var2 = int(input('Enter another number: '))
printNum = newFun(var1, var2)
print("The sum of the two numbers is: ", end="")
print (printNum)

In the first two lines of code, the user is asked to input two numbers, which are then stored in the corresponding variables. In the third line, the function is called, and its return value is stored in another variable called printNum. Lastly, the fourth and fifth lines print a string and printNum.

In conclusion, next time write a program, or even bake cookies, you know functions can serve as reusable pieces of code to make your code more efficient.

That's all for today! Until next time.
@emamex98

  1. Definition by Tutorials Point.