Python Functions

What Are Functions?

A function is a reusable block of code that performs a specific task.

Functions help you:

  • Avoid repetition by writing code once and using it many times
  • Organize programs into smaller sections
  • Make code easier to debug because each function has a focused job
  • Improve readability by giving tasks clear names
Loading...
Output:

Defining and Calling Functions

In Python, functions are created using the def keyword.

A function does not run automatically. You must call the function by writing its name followed by parentheses.

  • def starts the function definition
  • Function name describes what the function does
  • Parentheses can hold parameters
  • Indented code belongs to the function body
Loading...
Output:

Parameters and Arguments

A parameter is a variable listed in the function definition.

An argument is the actual value passed into the function when it is called.

You can:

  • Create one parameter for one input value
  • Create multiple parameters for multiple input values
  • Reuse the same function with different arguments
Loading...
Output:

Return Values

The return keyword sends a value back from a function.

This is different from print(). Printing only displays a value, while returning lets you store and reuse it.

You can:

  • Return numbers, strings, booleans, lists, dictionaries, or other values
  • Store returned values in variables
  • Use returned values in calculations or other function calls
Loading...
Output:

Print vs Return

print() displays information to the screen. return gives a value back to the program.

Use return when you need the result later.

Loading...
Output:

Default Parameters

A default parameter gives a parameter a backup value.

If the function is called without an argument, Python uses the default value.

You can:

  • Make arguments optional
  • Provide fallback values
  • Still override defaults by passing an argument
Loading...
Output:

Keyword Arguments

Keyword arguments let you pass values using parameter names.

This makes function calls easier to understand, especially when there are multiple parameters.

You can:

  • Pass arguments out of order
  • Make function calls more readable
  • Avoid confusion when parameters have similar data types
Loading...
Output:

*args

*args lets a function accept multiple positional arguments.

Python stores these extra arguments inside a tuple.

You can:

  • Accept an unknown number of values
  • Loop through the values
  • Use it for flexible calculations
Loading...
Output:

**kwargs

**kwargs lets a function accept multiple keyword arguments.

Python stores these arguments inside a dictionary.

You can:

  • Accept flexible named values
  • Work with dictionary-style data
  • Create reusable functions for profiles, settings, or records
Loading...
Output:

Scope

Scope describes where a variable can be used.

A variable created inside a function is called a local variable. A variable created outside a function is called a global variable.

You can:

  • Use local variables only inside the function
  • Use global variables outside functions
  • Avoid naming conflicts by understanding scope
Loading...
Output:

Practice: Grade Average Function

Prompt: Create a function called calculate_average that takes a list of grades and returns the average. Then, use the function to print each student's name and average grade.

Target Output:

Alice: 87.5
Bob: 91.5
Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic