🧩 Functions & Errors

Functions

फंक्शन

⏱ 4 hr3 topicsInteractive
🎯 By the end: You can define and call functions, pass positional/default/keyword arguments, return single or multiple values, and explain local vs global scope.

As programs grow, repeating code everywhere becomes a nightmare. A function is a named block of code you write once and reuse — the foundation of modularity. You've already used built-in functions like print() and len(); now you'll write your own. Every example here runs in the playground, so you can experiment as you read.

1Defining and calling a function

You define a function with the def keyword, give it a name and parameters in brackets, and indent its body. Nothing happens until you call it by name:

def greet(name):        # 'name' is a parameter
    return "Hello, " + name

message = greet("Asha")  # "Asha" is the argument
print(message)
Parameter vs argument: the parameter is the variable in the definition (name); the argument is the actual value you pass in when calling ("Asha"). Functions come in three families: built-in (print, len), module functions (math.sqrt), and user-defined (the ones you write).
Define and call▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Define a function with def name(parameters): and an indented body; call it by name with arguments.
  • Parameter = the name in the definition; argument = the value passed when calling.
  • Functions are built-in, from modules, or user-defined; they give you modularity (write once, reuse).

2Passing parameters: positional, default, keyword

There are three ways to pass arguments:

  • Positional — matched by order: power(2, 10) sets base=2, exp=10.
  • Default — a parameter can have a fallback value used when no argument is given: def power(base, exp=2). So power(5) uses exp=2.
  • Keyword — named explicitly, so order doesn't matter: power(exp=3, base=2).
Rule: in a definition, default parameters must come after non-default ones. And in a call, positional arguments must come before keyword arguments.
Positional, default & keyword arguments▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • Positional arguments are matched by order; keyword arguments are matched by name (order-free).
  • Default parameters supply a fallback value when no argument is given.
  • In a definition, defaults come after non-defaults; in a call, positional come before keyword.

3Return values and variable scope

A function sends a result back with return. It can return a single value, multiple values (packed into a tuple), or nothing — a function with no return automatically returns None.

Scope: local vs global

A variable created inside a function is local — it exists only during that call and can't be seen outside. A variable defined at the top level is global. To change a global variable from inside a function, you must declare it with the global keyword:

count = 0
def increment():
    global count   # without this, count would be treated as local
    count += 1
Multiple returns & scope▶ runs real Python
main.py
Output
Click Run to execute.
Key points
  • return sends a value back; returning several values packs them into a tuple.
  • A function with no return statement returns None.
  • Local variables live only inside a function; use the global keyword to modify a global from within.

★ Project: a small function library

In the playground, write and test these functions:

  1. is_even(n) that returns True/False using the % operator.
  2. area_rectangle(length, width=1) using a default parameter, then call it both ways.
  3. stats(numbers) that returns the minimum, maximum and average as three values, and unpack them.
  4. Bonus: a function with no return — print its result and confirm it shows None.

Ready for the chapter test?

Answer 5 questions. Score 60% or more to mark this chapter complete.

Start the test →

💡 Log in to save your progress and earn the certificate.