INTRODUCTION TO FUNCTIONS AND ‘IF’ CLAUSES!

Hello, my name is Manuel and I run ManyProgramming™. Today I will be introducing ‘functions’ and ‘if’ clauses to you. I hope you enjoy this blog! 

What are Functions? Functions are handy collections of code that can be re-used by calling the name of it! To define a variable you need to use ‘def’.

>>> def question_add(x=3,y=8):

             s = "What is " + str(x) + " + " + str(y) + "? "

             ans = input(s)

             if int(ans) == x + y:

                            print("correct!")

             else:

                            print("Wrong wrong wrong!")
How can you use this function?
>>> question_add()
What is 3 + 8? 11
correct!

>>> question_add(5,10)
What is 5 + 10? 14
Wrong wrong wrong!
If you run this function than you will see that it asks you to add 2 numbers. If the brackets are empty it will use ‘3’ & ‘8’ because I have pre-defined it. However, if you put two numbers inside of the brackets after ‘question_add, than the question will ask whatever 2 numbers you used.If you get the answer correct than it will say “correct!” However if you get it wrong, than it will say “Wrong wrong wrong!”. These are the very basics of my first project: Programming a learning website! We will need to use these in the website which we make.
Functions can return a value:
>>> def anything_you_want(x):

    return x * 2 + 5




>>> anything_you_want(3)

11

>>> y = anything_you_want(4)

>>> y

13
Now, what I have learnt about indents is that they always have to be the same size e.g. if you use 2 spaces than you have to continue using 2 spaces. You can change it if you are talking about a different function but when using the same function than you need to use the same number of indents every time. Now consider this example:
>>> def test()
   print('lolol')
print('lol')

>>> test()
lolol
Ok, so if you are wondering why it only printed ‘lolol’ instead of ‘lolollol’ is because after ‘def test()’ the print line had 3 indents and then the bottom line had 0 indents. Python thinks that the definition is finished if the indent number is not the same! WARNING: You will get an error if you do this!
Python has a number of built-in functions that you can use in your code. These are helpful tools that let you do lots of tasks, from imputting information and showing messages on the screen to converting one type of data into another. You’ve already used some of Pythons built-in functions such as “print ()” and “input ()”. Have a look at these examples. Why not try them out in a shell.
Now, let’s talk about ‘if’ clauses. They can help you to make decisions! For Example:
>>> quest = input('Do you speak German? y/n ')
Do you speak German? y/n N
>>> def question():
...     quest = input('Do you speak German? y/n ')
...     if quest.upper() == 'Y':
...         print('Guten Tag')
...     elif quest.upper() == 'N':
...         print('Buon Giorno')
...     else:
...         print('Please type Y/N')
...
>>> question()
Do you speak German? y/n n
Buon Giorno
>>> question()
Do you speak German? y/n N
Buon Giorno
>>> question()
Do you speak German? y/n a
Please type Y/N

Now as you can tell I have just defined what ‘question’ is. I said that if they answer ‘y/Y’ which stands for yes it will print Guten Tag. Otherwise if they answer ANYTHING else (gggdsajgyh, yy, nodh, nopped), it can literally be anything, than it will answer ‘LOSER!!!!!!!!!!!!!!!!!!!!!!’.

Now with the function name. You can have anything you want as the function name as long as there are no spaces. You can use an underscore _ to fill in for spaces. Alternatively use no spaces at all e.g. Anything you want (Not-Allowed) e.g. Anything_you_want (Allowed) Now one word is also allowed e.g. anythingyouwant. You are also not allowed to have a number at the beginning on the function name e.g. 1xx. If the number is at the end or in the middle then that it is alright!
Thank you for reading my blog and I hope you stay tuned every Friday for new Python blogs. I hope you enjoyed reading my blog and I hope it helps you. If you enjoy reading and would like to see my reading blog then Click Here!

Leave a comment