Project 8: Defining Functions

--Originally published at TEC GDL 2016

As I have covered in my last blog posts, Python has several built-in functions that can be called repeatedly to make your program run & look smoother. Today I will cover those functions created by users (user-defined functions), meaning ME & YOU:

How to define a function:?

Functions are created by (1) using the keyword def, followed by the function name and parentheses ( ). Inside the parentheses, you can (2) place the input parameter/arguments.
After that, the first statement can be an (3) optional statement: the documentation string (or ‘docstring’). The code block within the function then (4) starts with a colon (:).
After this, the function is exited by using the statement return and hopefully passes an expression (solution to the problem) to the user.
In short:

def function_name():

Defining a function does not mean that it is actually executed. In order for this to happen, the user has to call the function, as seen in my last blog post.
This time I will try to exemplify how functions are defined/created and then called by using another calculator program (just as in my Project 7). But this time, the functions are defined.
Have a look:

Bildschirmfoto 2016-09-15 um 00.19.35.png

In the part above, which is the beginning of our calculator.py program, all we do is define the functions. This is done by following the steps described earlier. When the function is defined you can decide what should happen to your parameters in the “return” step. For each mathematic operation used in this program we code  x + or – or * or /.
Let’s have a look what happens when these functions are called later:
Bildschirmfoto 2016-09-15 um 00.42.23.png

In the leftover part, the program first asks the user to decide which kind of operation he/she would like to complete. By using the built-in

?
‘input’, a number between 1 and 4 can be entered and is then “saved” in our “choice” variable. The program then uses an if, elif and else operator. These operators will be explained later on in my blog.
What is important to note is that once the choice has been done the program selects the two numbers to be calculated from the user. After collection of the numbers, the program automatically prints the calculation and most importantly also calls the defined function from part 1 and prints this at the end of the statement.

I hope this helped you to understand better how to define functions. As of this point, I am not too familiar with the different possibilities I have when defining functions but I hope to get better at this within the next weeks.
For another good example and really step-by-step explanation please visit THIS BLOG POST.

As always, if there are any questions remaining, please don’t hesitate to write me via Twitter or comment on this post.?

Best,
@tecjames

 

Sources:
http://www.programiz.com/python-programming/examples/calculator
http://www.tutorialspoint.com/python/python_functions.htm