Academy

Functions

Writing the Same Thing Twice Is a Warning Sign

Suppose your program needs to calculate a 10% discount in three different places. You could write price * 0.9 each time. But if the discount rate changes to 15%, you must find and update every single occurrence. Miss one and your program gives wrong answers silently.

This is the problem that functions solve. A function is a named, reusable block of code. You write it once. You call it by name wherever you need it. If the logic needs to change, you change it in one place.

This principle has a name in programming: Don't Repeat Yourself — DRY. Functions are the primary tool for achieving it.

Defining a Function

def greet():
    print("Good morning!")
  • def tells Python: I am defining a function.
  • greet is the name you choose.
  • The colon and indentation work exactly as they do in if and for — the indented block belongs to the function.

Defining a function does nothing by itself. The code inside doesn't run until you call it.

Three calls, three greetings. The function body runs each time. The definition above it runs zero times — it just registers the function so it can be called.

Parameters — Giving Functions Information

A function that always does the exact same thing has limited use. Parameters let you pass information in, so the function can behave differently each time.

name is a parameter — a variable that exists only inside the function. When you call greet("Priya"), Python puts "Priya" into name for that call. When you call greet("Arjun"), it's "Arjun". Each call gets its own copy.

You can define as many parameters as you need, separated by commas:

The values you pass when calling — "notebook", 45 — are called arguments. Parameters are the placeholders in the definition. Arguments are the actual values at call time. People often use the words interchangeably, and that's fine.

Return Values — Getting Information Back Out

So far, functions only print things. But usually you want a function to produce a value that you can use elsewhere. The return statement does this.

return a + b sends the value back to wherever the function was called. The call add(10, 5) produces 15, which is then stored in result.

A function with return is like a machine with an output slot — you put things in through parameters, it does something, and hands a result back through return.

Notice the second call: the return value is passed directly into print() without storing it in a variable first. You can do this whenever you don't need the value later.

return Exits the Function Immediately

As soon as Python hits a return statement, the function ends. Nothing after it runs.

If age >= 18, the function returns "Adult" and stops right there. The second return is only reached when the condition was False. This is a clean pattern — no else needed when you use early returns.

Scope — What Variables Live Where

Variables created inside a function exist only inside that function. They disappear when the function ends.

The last line crashes with NameError. result was created inside calculate() and lives only there. Once the function ends, it's gone.

This is called scope — the region of a program where a variable exists. Variables inside a function have local scope. Variables outside have global scope.

This is a feature, not a flaw. It means functions are self-contained — they don't accidentally interfere with variables elsewhere in your program.

Default Parameter Values

You can give a parameter a default value. If the caller doesn't provide that argument, the default is used.

greeting has a default of "Hello". The first call doesn't provide it, so the default is used. The second call provides its own, overriding the default.

Parameters with defaults must always come after parameters without defaults.

Putting It Together

Two functions, each doing one clear job. print_evens calls is_even — functions can call other functions. This is how all real programs are structured: small, named pieces of logic that work together.

What You Have Learned

Functions are the most important organisational tool in programming.

The key ideas:

  • def name(): defines a function; calling name() runs it
  • Parameters let you pass values in; arguments are the values you pass
  • return sends a value back to the caller and exits the function immediately
  • Variables inside a function are local — they don't exist outside it
  • Default parameter values make arguments optional
  • Functions can call other functions

In the next lesson, you'll take a closer look at strings — the most common data type in real-world programs — and learn the powerful tools Python gives you to work with them.