Functions in python

--Originally published at Sierra's Blog

Functions in Python are used to avoid being repeating the same code on multiple ocassions. It saves time, it needs less effort from your part, in a nutshell, it is useful.

How can we use it? 

The syntax looks like this:

def the_name_of_the_function  (parameters):

whatever that you want the function to do

function1

As an example I made a mix with some of the things that we’ve already learned to make a really simple “game” were the user needs to make a decision:

funcitions1

we start by creating 2 functions without parameters (because we won’t need them in this example)

funcitions2

then we add the code that’s going to make the program ask the user for a decision, which car does he want to use?

funcitions3

Then we add the conditionals that’s going to tell python  what’s what he needs to do if a certain thing happen.

 

Thanks for watching