A better look on functions. How do they work and when can you use them

--Originally published at Nihilistic Kirby

The first time I was coding I totally felt like a pro; however, everything changed when Ken told me I was kind of a mess, because I wrote too much and nothing had order. Then he explained to me one of the best things that can be done in a program, A FUNCTION. Yes, a function, this little (or larger) thing can do wonderful things for you when you’re coding and need to repeat some algorithm many times. You can also write something really cool that someone else can use inside its code.

When you create a function, it must start with a “>>>def …():” (short for define), this indicates that the whole stuff under that command will be a part of that function and not part of the main code, which can include a lot of variables without affecting the functionality of the whole code. One you define a cool name for you function, you must write something that will be done with respect of some variable (or not). I constantly use functions inside python codes when I feel like an artist and want to create cool patterns using slashes and stuff, that a good idea, instead of printing a lot of lines.

This is a basic example for a function that states 4 variables inside of it that can be changed inside of it by the user of the function, when the function is being called:

def sumsub(a, b, c=0, d=0):
    return a - b + c - d

print(sumsub(12,4))
print(sumsub(42,15,d=10))