Skip to main content
Translate content

To read content in another language, use your browser’s Translate page option (right-click the page or open the browser menu). Choose your preferred language; if offered, enable automatic translation. The site language menu changes navigation, not the original article. Interactive activities may remain in their original language.

PythonFundamentalsEstimated time: 45 minUpdated November 27, 2026

Python Functions for Beginners: Parameters, Return Values, and Scope

Learn Python functions, parameters, arguments, return values, and variable scope with interactive examples and step-by-step coding exercises.

Functions are one of the most important building blocks of Python programming. They allow you to organize instructions, reuse code, and solve complex problems through smaller, manageable tasks. In this beginner-friendly lesson, you will learn how to define and call functions, work with parameters and arguments, return values, and understand local and global variable scope through practical examples and interactive learning activities.

Visual guide to Python functions showing parameters, arguments, return values, and variable scope.

Learning objectives

  • Understand the purpose of functions in Python.
  • Define functions using the def keyword.
  • Call functions to execute reusable instructions.
  • Create functions with multiple parameters.
  • Return values using the return statement.
  • Understand the difference between print and return.
  • Use default parameter values.
  • Pass arguments using parameter names.
  • Distinguish local variables from global variables.
  • Recognize common function-related programming errors.
  • Apply functions to practical programming problems.

Introduction

A function is a reusable block of code. Instead of repeating instructions, define a function once and call it whenever needed. This lesson assumes you know variables, arithmetic operators and print().

Learning objectives

  • Define and call functions.
  • Distinguish parameters from arguments.
  • Return and reuse results.
  • Understand local and global scope.

1. Define and call a function

Use def, a function name, parentheses and a colon. Indent the body. Defining a function does not execute its body; calling it does.

def greet():
    print("Hello!")

greet()

Output: Hello!

2. Parameters and arguments

A parameter is a name in the function definition. An argument is a value supplied during a call.

def greet(name):
    print("Hello,", name)

greet("Sam")

Python binds the argument "Sam" to the parameter name. Output: Hello, Sam.

3. Returning values

return ends the function call and supplies a result to the caller. Unlike print(), it lets another expression reuse the result.

def rectangle_area(width, height):
    return width * height

area = rectangle_area(4, 3)
print(area)

First width becomes 4 and height becomes 3. The function calculates 4 * 3 and returns 12. The caller stores 12 in area and prints it.

Interactive experiment: Follow a function call

Open the interactive activities below

4. Default and keyword arguments

def greet(name, message="Hello"):
    return message + ", " + name

print(greet("Sam"))
print(greet(name="Sam", message="Welcome"))

The first call uses the default message and prints Hello, Sam. The second supplies both arguments by parameter name and prints Welcome, Sam.

5. Local and global scope

A name assigned inside a function is normally local to that function. A global name is defined at module level. A function can read a global name, but assigning a name inside a function normally creates a local binding.

score = 10

def example():
    score = 20
    return score

print(example())
print(score)

The outputs are 20 and 10. The local score does not change the global score. Prefer parameters and return values over changing global variables.

6. Print versus return

def show_double(n):
    print(n * 2)

def double(n):
    return n * 2

result = show_double(5)
print(result)
print(double(5) + 1)

The outputs are 10, None, and 11. show_double prints 10 but has no explicit return, so its result is None. double returns 10, allowing addition.

7. Practice exercises

  1. Define and call a function named welcome that prints Welcome!
    Solution
    def welcome():
        print("Welcome!")
    welcome()
  2. Write a function square(n) that returns the square of n.
    Solution
    def square(n):
        return n ** 2

    For square(5), n is 5 and the result is 25.

  3. Write a function add(a, b) and predict add(7, 4).
    Solution
    def add(a, b):
        return a + b

    The arguments bind to a and b, so the result is 11.

  4. Calculate rectangle_area(8, 2).
    Solution

    8 * 2 = 16.

  5. Write a function greet(name, message="Hi") that returns a greeting. Call it with only a name.
    Solution
    def greet(name, message="Hi"):
        return message + ", " + name
    print(greet("Alex"))

    Output: Hi, Alex.

  6. In the scope example, what is the global score after example() runs?
    Solution

    10, because the assignment inside example creates a local name.

  7. What does a function without an explicit return return?
    Solution

    None, provided the function finishes normally.

  8. Use double(6) from the worked example to calculate double(6) + 3.
    Solution

    double(6) returns 12; adding 3 gives 15.

Common mistakes

  • Forgetting the colon or indentation.
  • Defining a function without calling it.
  • Passing the wrong number of arguments.
  • Confusing print with return.
  • Trying to access a local name outside its function.

Key takeaways

Functions make programs reusable. Parameters receive arguments, return supplies results, and local scope keeps function variables separate from global names.

Interactive activities

Activities are grouped here and remain in their original language.

Definitions

Function

A reusable block of code designed to perform a specific task.

Function Definition

The declaration of a function using the def keyword, a function name, parameters, and an indented body.

Function Call

An expression that invokes a function and executes its body.

Parameter

A name declared in a function definition that receives a value when the function is called.

Argument

A value or expression supplied to a function during a call.

Return Value

The result supplied by a function to its caller.

Return Statement

A statement that ends the current function call and optionally supplies a result.

Default Parameter Value

A predefined value used when the caller does not supply an argument for that parameter.

Keyword Argument

An argument supplied using the corresponding parameter name.

Local Variable

A variable belonging to a function's local scope.

Global Variable

A variable defined at the module level.

Scope

The region of a program in which a name can be accessed according to Python's name resolution rules.

None

A special Python value representing the absence of a meaningful result.

Indentation

Whitespace at the beginning of a line that defines the structure of a Python code block.

Common mistakes to avoid

  • Forgetting the colon after a function definition.
  • Using incorrect indentation inside a function.
  • Forgetting parentheses when attempting to call a function.
  • Confusing parameters with arguments.
  • Passing an incorrect number of arguments.
  • Expecting a printed value to be automatically returned.
  • Placing a required parameter after a parameter with a default value.
  • Trying to access a local variable outside its function.
  • Assuming that assigning a local variable changes a global variable with the same name.
  • Using unnecessary global variables instead of parameters and return values.
  • Forgetting that return ends the current function call.

Key takeaways

  • Functions organize programs into reusable blocks of code.
  • The def keyword defines a function.
  • A function call executes the function body.
  • Parameters are names declared in function definitions.
  • Arguments supply values during function calls.
  • Functions can accept zero, one, or multiple arguments.
  • The return statement supplies a result to the caller.
  • The print function displays information but does not return the displayed value.
  • A function without an explicit return returns None when it finishes normally.
  • Default parameter values make some arguments optional.
  • Keyword arguments identify parameters by name.
  • Local variables belong to a function's local scope.
  • Global variables are defined at module level.
  • Parameters and return values help reduce unnecessary global state.
  • Functions improve code organization, readability, and reusability.