Plenty of π
Module 4: Data Collections and Functions
Functions

Functions are reusable blocks of code that perform a specific task. They help organize code and make it more modular.

  • Defining: Use the def keyword. def greet(name): print(f"Hello, {name}!")
  • Calling: Use the function name followed by parentheses with arguments. greet("Bob") # Output: Hello, Bob!
  • Parameters: Variables listed inside the parentheses in the function definition (e.g., name in greet(name)).
  • Arguments: Values passed to the function when it is called (e.g., "Bob").
  • return statement: Used to send a value back from the function. If omitted, the function returns None by default. def add(x, y): return x + y result = add(5, 3) # result is 8
  • Scope: Variables defined inside a function (local scope) are not accessible outside it, unless declared global (generally avoided).