I'm just starting my adventure with Python. Unfortunately, I can't figure out why, at the end of my code, I have to add myfunc()
. Without it my code doesn't display.
How is it if I use more than one definition then I have to put each definition at the end of the code?
def myfunc(a=4,b=6):sum = a + bprint(sum)myfunc()
The thing throwing you off is that you don't need to put all your code into a function. Your code could (and should) be rewritten as this:
sum = a + b
print(sum)
This code will do the exact same thing. A function, by definition, is a block of code that is given a name, so you can use it multiple times without having to rewrite the whole block of code every time you want to use it. Because you are putting your code into a function, you must tell Python that you want to run that code too.