πŸ–₯️
Berkeley CS61A: Notes
  • 🌍Hello, World!
  • πŸ’­Learn to Think Like Your Computer
  • Building Blocks
    • ❗Expressions in Python
      • ℹ️Mathematical Statements
      • 🀫Variables
      • πŸ‘Data Types in Python
    • 🚧Introduction to Functions
      • πŸƒβ€β™€οΈHow Python Executes a Function Call
      • 🚱Functions that Don't Return Anything
    • 🌊Control Flow
      • ⁉️What is an if statement?
      • πŸ’ΎWhat is a Loop?
      • 🚨What are Logical Operators?
        • πŸ˜–Logical Operators, Seemingly Illogical Behavior
      • 🚰How Does Control Flow?
    • ⚑Higher-Order Functions
      • ⏸️What are Higher-Order Functions Used For?
      • πŸ‘†Self-Reference
    • πŸ‘ΎEnvironments and Scope
      • πŸ–ŠοΈHow to Make Environment Diagrams
  • STRUCTURES OF DATA
    • ➰Recursion
      • πŸ«€Anatomy of Recursion
      • β›½Recursive Leap of Faith
    • πŸŽ„Tree Recursion
      • ⬇️The Use It/Lose It Principle
    • πŸ“€Iterables
      • βœ‚οΈList Slicing
      • πŸ’€Deep Lists
      • πŸŒ‹Iterable Functions
      • πŸ’½List Comprehensions
    • πŸ™ˆAbstraction
    • Trees
    • Linked Lists
    • Iterators and Generators
    • Efficiency
  • Extra Topics
    • 🎭Errors and their Types
      • 🏹Error vs Exception
      • πŸ”™What is Backtracing?
    • 🚑How the Terminal and the File are Different
    • 🍬Decorators
  • Debugging Tools
    • πŸ–¨οΈDebugging with Print Statements
    • πŸ›Debugging with the Debugger
Powered by GitBook
On this page

Was this helpful?

  1. Building Blocks
  2. Introduction to Functions

Functions that Don't Return Anything

A curious sub-category of functions in Python.

A function, typically, returns a value. Keyword: "typically". Consider a function like so:

def f(x):
	print(x)

This is a completely valid function that takes in a value x and prints it. Notice how this function does not return anything.

Now, for a contradictory fact: all Python functions return a value.

How does this work? When we don't return any value, Python does it for us. Let's try this in our interpreter.

>>> def f(x):
>>> ...  print(x)
>>> r_val = f("hi")
hi
>>> r_val
>>>

You'll notice how prompting r_val on the terminal does not return anything β€” this is because r_val is None.

This, we now know a deep fact about Python β€” that it implicitly returns None when no other value is returned.

def f(x):
	print(x)
	# return None # implicitly!
PreviousHow Python Executes a Function CallNextControl Flow

Last updated 3 years ago

Was this helpful?

🚧
🚱