Python For Beginners

Python For Beginners

Logic blocks with Python Functions

Within the previous lessons, we made a simple program which checks if the number is negative or not.

  • Now, there is a high probability that we will want to use this logic many times in our code.
  • With our current approach, we would have to copy paste complete program every-time when we need it.

That wouldn’t make much sense now, would it.

This is why programming languages (including Python) have functions.

Some vocabulary:

  • function – a way to take a part of the program, and assign it to some common name ‘function name’.
    • Then, when you need this logic, you simply call function name (and it will execute all the lines of code inside of the function).
  • parameters – values which are somehow related to the function
  • input parameters – values that function can accept.
    • In general, functions can be without parameters, or they can accept one or more parameters.
    • There is a different story about how many parameters a function should have, but that is more advanced topic.
  • return parameter – value that will be returned by the function
  • docblock – comment that is describing a function completely

1.4.1. Lets rewrite out previous code and make a function out of it

As we did so far please open https://www.onlinegdb.com/online_python_compiler and code this example.

# 1. We define our function, make a name for it and program the logic

def is_integer_negative(x):
    """
    Check if the integer is negative
    
    Keyword arguments:
    x - integer which needs to be checked
    """
    
    if x < 0:
        print('The number you have entered is negative')
    elif x == 0:
        print('The number you have entered equals zero')
    else:
        print('The number you have entered is positive')
        
# 2. We take the values from the console and store them to memory
x = int(input("Please enter an integer: "))

# 3. We call our function with the values from memory (which we picked up from the console)
is_integer_negative(x)

Once you retype it (don’t copy paste it), it should look like this in your editor.

is integer negative function

Homework: There is an error within this screenshot, find it, comment below and earn Codies.

Explanation of the example

  • Between lines 4-18 we have created our function.

When we want to make new function, we start a code line with ‘def’ keyword. It is short for a ‘define’. After def we usually write function name, followed by the ().

Inside of the () we can write input parameters. In our case we used (x), which means that function has one input parameter which is saved in memory under a label/variable name ‘x’.

Later when we call this function, line 24, we will use this name and store something in this variable.

  • Function has a docblock between lines 5-10.

This part of the code is just a description of the function and it is not doing anything. It will not be executed.

It is used by the other developers, to quickly learn about your function if they use it.

Same goes for the other way, if you are using someones function then you will most likely read docblock (if there is one to read).

  • At line 21, we are taking an input from console

This is something that we did multiple times so far.

  • At the line 24, we are calling our function which is then executed.

Note: It is important to understand that function can be called multiple times.

So, we define functions once (lines 4-18) but we can call it lots of times (line 24 can be used lots of times, where instead of ‘x’ we can use some other value).

 

1.4.2. Exercise

Look at this code example and try to understand will the result be.

# 1. We define our function, make a name for it and program the logic

def is_integer_negative(x):
    """
    Check if the integer is negative
    
    Keyword arguments:
    x - integer which needs to be checked
    """
    
    if x < 0:
        print('The number you have entered is negative')
    elif x == 0:
        print('The number you have entered equals zero')
    else:
        print('The number you have entered is positive')

        
# 2. We take the values from the console and store them to memory
x = int(input("Please enter an integer: "))

# 3. We call our function with the values from memory (which we picked up from the console)
is_integer_negative(x)

# 4. Lets call our function few more times

is_integer_negative(3)
is_integer_negative(-2)
is_integer_negative(x+1)
is_integer_negative(x+x)
is_integer_negative(x-x)

For example, if we enter integer 10, these will be the results:

Python results with 10
Python results with 10

Why did we get results like this?

  • Line 27 – function is calling 3 which is positive
  • Line 28 – function is calling -2 which is negative
  • Line 29 – function is calling x+1 , which is calculated as 10+1 = 11 and 11 is positive
  • Line 30 – function is calling x+x, which is calculated as 10+10 = 20 and 20 is positive
  • Line 31 – function is calling x-x, which is calculated as 10-10 = 0 which equals zero

1.4.3. Homework

  • What will the results be if we enter some other integer, for example -1 ?
  • Will function at line 32 ever had any other result than ‘equals zero’ ?
  • Can you change the name of this function, but make sure that program is still working?
Leave a Comment