Skip to main content
Welcome back. Your dashboard works, but it’s getting messy. Everything’s lumped together, and when you want to reuse functionality you end up copying and pasting. Functions solve this problem. Think of a function like a button: whenever you need a piece of behaviour, press the button (call the function). Functions keep code tidy, reusable, and easier to grow and maintain. In this article we’ll learn how to define and call functions in Python and then apply them to clean up a simple dashboard. By the end you’ll be able to break large programs into small, reusable parts.
The image shows a person in a "KodeKloud" shirt standing next to a digital interface with various dashboard functions like "Energy," "Mood," and "Goal Tracking." It's likely part of a presentation or instructional video.

What is a function?

A function is a named block of code that does one job. Python includes many built-in functions such as print(), len(), and input(). In this lesson you will create your own functions to encapsulate behaviour and return values. Why use functions?
  • Remove repetition (Don’t Repeat Yourself — DRY).
  • Make intent explicit with descriptive names.
  • Make code easier to test and maintain.
  • Improve readability by expressing program flow at a high level.
For reference, see the official Python docs on functions: https://docs.python.org/3/tutorial/controlflow.html#defining-functions

Example: repeated code vs. a function

Imagine you prompt the user for a rating in many places:
Running this script might produce:
Instead of repeating that block, define the behaviour once and call it multiple times:
When the interpreter reaches each get_score() call it runs the code inside the function. This is the essence of reuse.

Important rule: define before you call

You must define a function before you call it in top-level script execution. Calling a function before it exists raises a NameError.
Always define your functions before you call them in a script.

Applying functions to the dashboard

A good pattern is to separate data (user variables) from behaviour (functions). Start by keeping user information at the top, then implement small functions that operate on that data. Example: define the user:
Create a function that collects the user’s mood. Note this function loops until it receives valid input and returns the value it collected:
Returning values is the typical way to pass data from a function back to the caller instead of relying on global variables. This makes the function easier to reuse and test.
Prefer returning values and passing them as parameters rather than relying on global variables — this avoids scope issues.

Sketching additional dashboard functions

A function should do one thing and do it well. Use action verbs for names so intent is clear:
  • get_energy()
  • show_wellbeing()
  • get_goals()
  • get_completions()
  • show_results()
  • show_final_message()
Here are simple example implementations for a few of those functions:
Note how each function has a single job: collect a value, compute a count, or display results.

Assembling the dashboard

Compose your program by calling functions in a clear, descriptive sequence. The main script reads like a flowchart:
Because functions encapsulate behaviour, the top-level script becomes easy to scan: each call is descriptive and documents the program flow.

Scope and NameError: why parameters matter

If a function expects to use values such as mood or energy, those values must be passed in as parameters or accessed via return values. Variables local to one function (or defined only in the main script scope) are not automatically visible inside other functions. Consider a buggy show_wellbeing() that tries to use mood without receiving it:
This runtime error shows variable scope problems. The fix is to either pass mood and energy as parameters to show_wellbeing(mood, energy) or have show_wellbeing() return values it needs, then use them in the caller.

Quick reference and best practices

Recap

  • Functions are named blocks of code that perform one task.
  • Define functions before calling them.
  • Use functions to remove repetition and clarify your program flow.
  • Prefer returning values and passing them as parameters to avoid scope issues and make functions reusable and testable.
The image features a slide with notes on using functions in code, accompanied by a cartoon cat and a person standing beside it.

Watch Video