Functions (Python3)

--Originally published at Elu's Blog

Functions in programming are like commands that you tell the computer. We already saw the int() and input() functions. For example, the input() function tells the computer to store anything that the user writes.

Creating a function

If you want to create a specific instruction for the computer, you’ll have to create a function. To do this, first you’ll have to tell the program that you’re defining a function by writing “def” before the function’s name. The next step is to write the name of the function. Then, you’ll have to tell the program if the function is going to receive any variable (that will be inside the brackets “()”), if not, don’t write anything inside the brackets. Next is to write a colon “:” to tell the function that the next things that are tabbed will be the core of the function.

Calling a function

To call a function means to merely use the name of the function and (if it is the case) write the variables needed.

Let’s look at an example:

Captura de pantalla 2017-01-31 a la(s) 15.55.33.png

In the image above we can see that the “myfunction()” function is being defined by writing “def” before it. We can also see that no variable is going to be needed in order for the function to work. The function itself only creates a variable “x” and assigns the 55 value to it, then it prints the value of “x”. After that, in line 5, that function is being called by just writing it’s name.

When you run that code, it will just simply print a number 55 on the screen. If in line 6 there was another caling for “myfunction()”, the program will instead print two number 55’s.

Another explanation

As always, if you didn’t understand my explanation or just didn’t like it, here is another

Continue reading "Functions (Python3)"