Andromeda
Note

Parameters (Python)

Definition

Parameters are the placeholders or variables defined in the function signature that receive the arguments when the function is called.

Why It Matters

Parameters define the “interface” or contract of a function, dictating what inputs it requires to do its job. Well-designed parameters make functions flexible and easier to use.

Core Concepts

  • Define: Creating the function with def and listing the parameters.
  • Parameter Variable: The internal variable name used within the function body to refer to the passed-in argument.
  • Default Values: Pre-defined values for parameters (e.g., def greet(name='Guest'):) that allow a function to be called with fewer arguments, simplifying the common case.
  • Standard Library Examples: Parameters like end='' and sep=',' in the built-in print() function showcase how default parameters modify behavior.
# Function with parameters and a default value
def describe_pet(animal_type, pet_name='Willie'):
    print(f"I have a {animal_type} named {pet_name}.")

describe_pet('hamster', 'Harry')  # Positional arguments
describe_pet('dog')              # Using default value

Connected Concepts