Skip to main content
Functions are reusable blocks of code that perform a specific task. They improve code readability, reduce duplication, and make programs easier to maintain.

Creating a Function

Use the def keyword.

Syntax

Example
Output

Calling a Function

A function executes only when it is called.
Output

Function Parameters and Arguments

Parameter

Variables defined in the function.
name is a parameter.

Argument

Actual value passed to the function.
"Alice" is an argument.

Positional Arguments

Arguments are matched by position.
Output

Keyword Arguments

Arguments are passed using parameter names.
Output

Default Parameters

A parameter can have a default value.
Output

Multiple Parameters

Output

Return Statement

Returns a value to the caller.
Output

Returning Multiple Values

Output

print() vs return

print()

Displays output.
Output

return

Returns a value.
Output

Arbitrary Arguments (*args)

Accepts any number of positional arguments.
Output

Arbitrary Keyword Arguments (**kwargs)

Accepts any number of keyword arguments.
Output

Combining Parameters

Output

Variable Scope

Scope determines where a variable can be accessed.

Local Scope

Created inside a function.
Outside the function:
Output

Global Scope

Created outside every function.
Output

global Keyword

Modify a global variable.
Output

Nested Functions

A function inside another function.
Output

Recursive Function

A function calling itself.
Output

Lambda Function (Anonymous Function)

Small one-line function.
Output
Multiple parameters
Output

Higher-Order Functions

Functions can be passed as arguments.
Output

Built-in Functions

Some commonly used Python functions. Example

Function Annotations (Type Hints)

Used to specify expected types.

Decorators

A decorator is a function that modifies or extends the behavior of another function without changing its original code. Decorators use the @ symbol.

Simple Decorator

Output

Decorator with Arguments

Output

Multiple Decorators

Decorators are applied from bottom to top.
Output

Common Built-in Decorators

@staticmethod

Does not use self or cls.

@classmethod

Receives the class (cls) instead of the object.

@property

Allows calling a method like an attribute.

Quick Revision

  • def → Create a function
  • Call function using ()
  • Parameters → Variables in function definition
  • Arguments → Values passed to function
  • return → Returns value
  • print() → Displays value
  • Default parameters → Optional values
  • *args → Multiple positional arguments
  • **kwargs → Multiple keyword arguments
  • Local scope → Inside function
  • Global scope → Outside function
  • global → Modify global variable
  • Nested function → Function inside another function
  • Recursion → Function calls itself
  • Lambda → Anonymous one-line function
  • Higher-order function → Takes or returns functions
  • Type hints → Specify expected parameter/return types
  • Decorator → Adds functionality without modifying the original function
  • @staticmethod → No self or cls
  • @classmethod → Uses class (cls)
  • @property → Access method like an attribute