Def spam


Lesson 6/13 What does def spam() means at the beginning of the code, and how it is used for printing


The def keyword is how we define a function. A function is a code segment (a block) that we can call up any time and have that piece of code execute.

def foo():
    print ("Foo!")

foo()    # Foo!

The top line is called the header or signature line. That tells Python what name to store the function with, and what parameters are expected by the function. In the above there are no expected parameters. Notice that the last line, which is is the function call expression statement uses empty parens to indicate no arguments (to match the no expected parameters).

The print statement inside the function is indented. This is important since it tells Python that code should execute when the function is called.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.