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.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

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.

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.

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

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

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

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

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

Scope determines where a variable can be accessed:

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

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.