SOLID STATE PRESS
← Back to catalog
Functions and Scope cover
Coming soon
Coming soon to Amazon
This title is in our publishing queue.
Browse available titles
Computer Science

Functions and Scope

A High School and Early College Primer for Programmers

You wrote a function. It runs. Then something breaks — a variable isn't found, a value comes back wrong, or a loop inside your function stomps on something outside it. Scope errors and function confusion are where most beginners stall, and a textbook chapter or YouTube video rarely fixes it cleanly.

**TLDR: Functions and Scope** is a focused, 10–20 page primer that cuts straight to what you need to know. It covers how to define and call functions in Python and JavaScript, how parameters and return values actually work (and why printing isn't returning), and how the LEGB rule determines which variable wins when two share a name. It walks through the call stack frame by frame so you can see exactly when variables are created and destroyed — including what happens during recursion. It closes with the pitfalls instructors see most often: mutable default arguments, accidental globals, and functions that try to do too much.

This guide is written for high school students in an intro programming or AP Computer Science Principles course, college students in CS 101, and anyone tutoring or helping a student get unstuck. If you're looking for a short programming reference for students that respects your time and skips the filler, this is it.

Read it once before your next lab or exam and walk in knowing exactly where your variables live.

What you'll learn
  • Define a function, call it with arguments, and return values correctly
  • Distinguish parameters from arguments and positional from keyword arguments
  • Trace the lifetime of a variable through local, enclosing, global, and built-in scopes
  • Predict what a piece of code prints by reasoning about scope and the call stack
  • Recognize and avoid common bugs caused by shadowing, mutable defaults, and unintended globals
What's inside
  1. 1. What Is a Function?
    Introduces functions as reusable, named blocks of code with inputs and outputs, and shows how to define and call them.
  2. 2. Parameters, Arguments, and Return Values
    Covers positional and keyword arguments, default values, multiple returns, and the difference between returning and printing.
  3. 3. Scope: Where Variables Live
    Explains local, enclosing, global, and built-in scopes and how the interpreter resolves names using the LEGB rule.
  4. 4. The Call Stack and Variable Lifetime
    Walks through how each function call creates a new frame, how variables are born and destroyed, and how recursion stacks frames.
  5. 5. Common Pitfalls and Best Practices
    Warns about mutable default arguments, accidental globals, shadowing built-ins, and over-long functions, with fixes for each.
  6. 6. Why It Matters: Functions as Building Blocks
    Connects functions and scope to larger ideas: modularity, testing, closures, and the patterns students will see in any language they learn next.
Published by Solid State Press
Functions and Scope cover
TLDR STUDY GUIDES

Functions and Scope

A High School and Early College Primer for Programmers
Solid State Press

Who This Book Is For

If you are taking a high school coding class and functions have started to feel slippery, or you are working through an intro CS class and need Python functions and scope explained simply, this book is for you. It also works for AP Computer Science Principles students, self-taught beginners, and parents helping a teenager debug homework at the kitchen table.

The book covers how functions are defined and called, how parameters and return values move data in and out, and how variable scope works in Python and JavaScript — including the call stack and scope behavior that trips up nearly every new programmer. These are the core beginner programming concepts any study guide worth reading must address. About 15 pages, no filler.

Read straight through to build the mental model, then work every example inline. If you want to learn Python functions for an intro CS class or just need a short programming reference for students before an exam, finish with the practice problems at the end.

Contents

  1. 1 What Is a Function?
  2. 2 Parameters, Arguments, and Return Values
  3. 3 Scope: Where Variables Live
  4. 4 The Call Stack and Variable Lifetime
  5. 5 Common Pitfalls and Best Practices
  6. 6 Why It Matters: Functions as Building Blocks
Chapter 1

What Is a Function?

A function is a named, reusable block of code that takes some input, does something with it, and (usually) hands back a result. Think of it like a vending machine: you put in a selection code, the machine runs its internal process, and a snack comes out. You don't need to know how the machine works internally — you just need to know what to put in and what to expect back.

Without functions, every program would be one long sequence of instructions. If you needed the same calculation in three places, you'd copy-paste it three times — and fixing a bug in that calculation would mean finding every copy. Functions solve this by letting you write the logic once, name it, and reuse it anywhere.

Defining a function

In Python, you define a function with the def keyword, a name, a pair of parentheses, and a colon. The indented block underneath is the function's body — the code that runs when the function is called.

def greet(name):
    message = "Hello, " + name + "!"
    return message

In JavaScript, the syntax is slightly different but the structure is identical:

function greet(name) {
    let message = "Hello, " + name + "!";
    return message;
}

Both versions do the same thing. The word name inside the parentheses is a parameter — a placeholder variable that stands in for whatever value the caller will provide. The return keyword sends a value back to whoever called the function.

Calling a function

Writing a function definition doesn't run it. It just registers the function under its name, ready to be used. To actually execute the code, you call (also said: invoke) the function by writing its name followed by parentheses containing any values you want to pass in.

result = greet("Ada")
print(result)   # Hello, Ada!

The value "Ada" passed at call time is called an argument. The parameter name receives that argument and acts as a variable inside the function body with the value "Ada".

Keep reading

You've read the first half of Chapter 1. The complete book covers 6 chapters in roughly fifteen pages — readable in one sitting.

Coming soon to Amazon