Python · functions

Function Basics

Name a block of code once, call it whenever you need it, pass values in and get a value back.

Simple function


          # define and call function
          def greet():
              print("Hello!")
          greet()
          # ans: Hello!
        

def only stores the function; its body runs when the function is called with ().

Function with a parameter


          # function with one parameter
          def greet_person(name):
              print(f"Hello, {name}!")
          greet_person("Alice")
          # ans: Hello, Alice!
        

The call creates a new frame on the stack, where the parameter name holds the argument "Alice".

Arguments in, return value out

add(5, 3)53
def add(a, b):
a = 5 b = 3
return a + b
result =8
call add with
  1. The arguments are copied into the parameters a and b.
  2. The body runs using those names.
  3. return sends the value back to where the call was made.

Function with multiple parameters


          # multiple parameters
          def add(a, b):
              return a + b
          result = add(5, 3)
          # ans: result is 8
        

Function with return


          # return value
          def square(x):
              return x ** 2
          result = square(5)
          # ans: result is 25
        

Function without return


          # no return (returns None)
          def print_message(msg):
              print(msg)
          result = print_message("Hi")
          # ans: Hi (printed)
          # ans: result is None
        

Printing shows a value on screen; it does not give the value back to the caller.

Exercises

Predict the answer, then press → to trace the code and check.

Exercise 1


          # what is returned?
          def func():
              x = 5
          func()
          # ans: None (no return statement)
        

Exercise 2


          # what is result?
          def add(a, b):
              return a + b
          result = add(10, 20)
          # ans: 30
        

Exercise 3


          # function call without ()?
          def greet():
              return "Hello"
          x = greet
          # ans: x is the function object, not "Hello"
        

Exercise 4


          # what happens?
          def multiply(x, y):
              return x * y
          multiply(5, 3)
          # ans: 15 (but not stored)
        

Exercise 5


          # can you call before defining?
          result = add(1, 2)
          def add(a, b):
              return a + b
          # ans: NameError (function not defined yet)
        

Exercise 6


          # function with print vs return?
          def func1():
              print(5)
          def func2():
              return 5
          x = func1()
          y = func2()
          # ans: 5 (printed)
          # ans: x is None, y is 5
        

Exercise 7


          # nested function call?
          def double(x):
              return x * 2
          result = double(double(5))
          # ans: 20
        

Exercise 8


          # what is printed?
          def test():
              return 5
              print("After return")
          test()
          # ans: 5 (return exits the function,
          # ans: so the print never runs)
        

At the interactive prompt the returned 5 is echoed; nothing is printed by the function itself.

Exercise 9


          # function name case?
          def MyFunction():
              return 1
          myfunction()
          # ans: NameError (names are case-sensitive)
        

Exercise 10


          # redefining function?
          def func():
              return 1
          def func():
              return 2
          func()
          # ans: 2 (second definition overwrites first)
        

Remember

Define, then call

def stores the function under its name; name() runs it. Calling before the def line is a NameError.

Each call gets a frame

Parameters are local names in that frame, filled from the arguments.

return ends the call

It hands one value back; code after it never runs. No return means None.

Functions are values

x = greet copies the function; x = greet() stores its result.