Functions: Naming Your Logic

📌 Concept 9 of 13✍️ Written by Mark Sullivan📅 Reviewed 2026-04-25⏱ ~14 min read

Once functions click, every other concept gets easier. They're how you turn 200 lines of duplicate code into 20 lines that read like prose.

Bee, our debugging mascot

From Bee: If your function appears to do nothing, you forgot return. Print the function call's result and you'll see undefined.

What Is a Function?

A function is a named, reusable block of code. You define it once with a name and a list of parameters; you call it many times, passing in different arguments; it does some work and optionally returns a value.

Functions are the lever that makes complex programs possible. A 1000-line program with 50 well-named functions reads like a story; the same program with no functions is unreadable.

Parameters and Arguments

Parameters are the names listed when a function is defined; arguments are the actual values passed in when you call it. Loose usage often blurs the two — most people use them interchangeably.

Return Values

Most functions return a value. Some don't — they just have a side effect like printing. Forgetting return is one of the most common beginner bugs: the function appears to work, but anything that asked for its result gets undefined / None.

Scope

Variables declared inside a function aren't visible outside it. The function gets its own little world. This is intentional — it's why you can have i inside two different functions without them colliding.

Call-Stack Visualizer

Functions stack up as they're called and pop off as they return. Step through to see how it works — including recursion.

Code Examples in Three Languages

// Definition
function greet(name) {
  return `Hello, ${name}!`;
}

// Call
console.log(greet("Aisha"));   // Hello, Aisha!

// Arrow function (modern)
const add = (a, b) => a + b;

// Default parameter
function multiply(a, b = 2) {
  return a * b;
}
multiply(5);   // 10  (b defaulted to 2)
def greet(name):
    return f"Hello, {name}!"

print(greet("Aisha"))

# Default parameter
def multiply(a, b=2):
    return a * b

multiply(5)        # 10
multiply(5, 3)     # 15

# Keyword arguments
def intro(name, age):
    return f"{name}, {age}"
intro(age=30, name="Aisha")
public static String greet(String name) {
    return "Hello, " + name + "!";
}

System.out.println(greet("Aisha"));

// Java requires types and a class context.
// Methods inside classes work the same way.
public static int add(int a, int b) {
    return a + b;
}

Best Practices

  1. Give functions clear, verb-based names: calculateTotal, not doStuff.
  2. Keep functions small — under 30 lines is a healthy ceiling.
  3. Each function should do one thing.
  4. Prefer pure functions: same input → same output, no side effects.

Common Mistakes

  • Forgetting return. Function appears to work; callers get nothing.
  • Long parameter lists. More than ~3-4 parameters? Pass an object instead.
  • Side effects nobody expected. A function called getUser shouldn't modify the database.
  • Mixing input and output. Reading from disk inside a calculation function makes it hard to test.
🐛
See the bugs in action

We have a dedicated Common Bugs with Functions page — five real broken snippets with the fix. Read it before you start writing.

How It Works Under the Hood

When a function is called, the runtime creates a stack frame: a chunk of memory holding the parameters, local variables, and a return address. The frame is pushed onto the call stack. When the function returns, the frame is popped — and the caller resumes at the saved return address.

This is why recursion has a depth limit: each call adds a frame; too many and you overflow the stack. Most environments allow a few thousand frames before crashing. Some languages optimize tail calls to avoid growing the stack; JavaScript engines mostly don't.

JavaScript closures work because each function captures a reference to the variables in its enclosing scope. The closure keeps those variables alive even after the outer function returns. This is the foundation of every callback, every event handler, every "currying" trick.

From Beginner to Pro

The same concept gets deeper as you grow. Here's what mastery looks like at three levels:

Beginner

You define function add(a, b), call it with add(2, 3), get back the sum.

Intermediate

You write small, single-purpose functions. You pass functions as arguments (callbacks). You use default parameters and destructuring.

Pro

You write pure functions where possible (no side effects), compose them with map/filter/reduce, and reach for memoization, partial application, and higher-order helpers when they make code clearer. You know when to use a method vs. a free function and respect the principle of least surprise.

Performance & Gotchas

  • Forgetting return — silent undefined / None.
  • Mutable default arguments in Python — shared across calls.
  • Arrow functions and this — arrow doesn't bind its own this.
  • Recursion without a base case — stack overflow.

Quick Quiz

Real-World Uses (Production Code, Today)

React components

Every React component is literally a function. function Button(props) { return ... }. Same idea you learned in lesson 9, used in production by millions of devs.

Lambda handlers

AWS Lambda runs your function whenever something happens. Your job: write one function. AWS calls it. Same shape — input, output.

Database query helpers

Every backend has functions like findUserById(id). Reusable, named, take input, return output. Functions, all the way down.

Frequently Asked Questions

What's the difference between a function and a method?

Methods are functions that "belong" to an object or class. Same idea, different attachment.

What's an arrow function?

JavaScript shorthand: (a, b) => a + b. Roughly equivalent to a regular function with subtle differences around this.

Can a function call itself?

Yes — that's called recursion. Useful for tree-shaped data and certain math problems.

What's a "pure function"?

One that returns the same output for the same input and has no side effects. Easier to test and reason about.

Can a function take another function as an argument?

Yes — that's a "callback" or "higher-order function." Common in JS for event handlers.

Do functions need a return statement?

No — some functions only have side effects (printing, saving). Without return they implicitly return undefined / None.

What's the difference between parameter and argument?

Parameters are listed in the definition; arguments are passed at call time. The terms are often used interchangeably.

How many parameters is too many?

3-4 is fine; 5+ is a smell. Group them into an object/dict instead.

Key takeaways
  • Functions: Naming Your Logic is one of the 13 universal concepts of programming.
  • The syntax differs across languages, but the underlying idea is the same.
  • Practice in the playground to make it stick.
Was this helpful?
M
Mark Sullivan
Lead writer · 8 yrs full-stack

Mark started coding in 2017 after switching from financial analysis. She's built production systems in Python (Django) and JavaScript (Node + React) at two startups, and has taught intro programming at his local community college since 2022. He owns the curriculum for variables, functions, conditionals, and loops on this site. More about Mark →