Functions

A function in Python is a named, reusable block of code that performs a specific task, optionally takes inputs (arguments) and returns a result, so you can run the same logic many times without repeating it.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Learn to organize, reuse, and simplify your code with Python functions.

What You'll Learn in This Lesson

1. What Are Functions?

A function is a reusable block of code that performs a specific task. Instead of writing the same code over and over, you write it once in a function and call it whenever you need it.

Why Use Functions?

2. Defining a Function

Part

Meaning

def

Keyword that starts a function definition

function_name

The name you give your function (use snake_case)

()

Parentheses for parameters (empty if none)

:

Colon to start the function body

Indented code

The code that runs when you call the function

3. Parameters — Giving Functions Inputs

Parameters are variables listed inside the parentheses when defining a function. They allow you to pass information into the function.

4. Return Values — Getting Results Back

The return keyword sends a value back to the code that called the function. This lets you use the result!

5. Default Parameter Values

You can give parameters default values . If no argument is passed, the default is used:

6. Positional vs Keyword Arguments

When calling a function, you can pass arguments in two ways:

Type

Example

Description

Positional

func("Alice", 25)

Arguments matched by position/order

Keyword

func(name="Alice", age=25)

Arguments matched by name (any order)

7. Returning Multiple Values

Python functions can return multiple values at once! They're returned as a tuple.

8. Variable Scope — Local vs Global

Scope determines where a variable can be accessed:

Scope

Where Defined

Where Accessible

Local

Inside a function

Only inside that function

Global

Outside all functions

Everywhere in the file

9. Docstrings — Documenting Your Functions

A docstring is a special string that describes what your function does. Put it right after the def line:

10. Common Mistakes to Avoid

Mistake

Wrong

Correct

Forgetting parentheses

greet

greet()

Forgetting colon

def greet()

def greet():

Missing indentation

def greet(): print("Hi")

Using print instead of return

print(a + b)

return a + b

Wrong argument count

add(5)

add(5, 3)

11. Practical Examples

Example 1: Temperature Converter

Example 2: Grade Calculator

Example 3: Simple Calculator

Summary: Quick Reference

Concept

Syntax

Define function

def name():

With parameter

def name(param):

def greet(name):

Return value

return value

Default param

param="default"

def greet(name="Guest"):

Keyword arg

param=value

greet(name="Alice")

Multiple returns

return a, b

x, y = func()

Docstring

"""text"""

"""Calculate area."""

Lesson 6 done — you can now write reusable code like a pro!

Functions are the single most important concept in all of programming. You now know how to define them, pass data in, get results out, set defaults, and return multiple values.

🚀 Up next: Lists — learn to store and manage collections of data, the backbone of most real programs.

Practice quiz

Which keyword is used to define a function in Python?

  • func
  • define
  • def
  • function

Answer: def. Functions are defined with the def keyword.

How do you call a function named greet that takes no arguments?

  • greet()
  • greet
  • call greet
  • def greet()

Answer: greet(). You call a function by writing its name followed by parentheses: greet().

What does a function return if it has no return statement?

  • 0
  • An empty string
  • An error
  • None

Answer: None. A function without a return statement returns None by default.

What is the difference between a parameter and an argument?

  • They are the same thing
  • A parameter is in the definition; an argument is the value passed in
  • An argument is in the definition; a parameter is passed in
  • Parameters are only for built-in functions

Answer: A parameter is in the definition; an argument is the value passed in. Parameters are the placeholders in the definition; arguments are the actual values supplied at call time.

For def greet(name='Guest'): what does greet() print?

  • Hello, Guest!
  • Hello, name!
  • An error
  • Hello, !

Answer: Hello, Guest!. With no argument the default value 'Guest' is used.

Where must default parameters appear in a function definition?

  • First, before non-default ones
  • Anywhere
  • After non-default parameters
  • Only alone

Answer: After non-default parameters. Default parameters must come after all non-default parameters.

What does 'return a + b, a - b' give back?

  • Only a + b
  • A tuple of both values
  • A list
  • An error

Answer: A tuple of both values. Comma-separated return values are packed into a tuple.

What is the main difference between print() and return?

  • No difference
  • return displays output
  • print() is faster
  • print() displays output (returns None); return sends a value back to use

Answer: print() displays output (returns None); return sends a value back to use. print() shows text but the function returns None; return hands a usable value back to the caller.

A variable defined inside a function is what kind of scope?

  • Global
  • Local
  • Universal
  • Static

Answer: Local. Variables defined inside a function are local and only accessible inside that function.

What is a docstring?

  • A required return value
  • A type of parameter
  • A string describing what a function does, placed right after def
  • A way to call a function

Answer: A string describing what a function does, placed right after def. A docstring is a string placed at the start of a function body to document it.

Continue this course