cf.completefrontendCode editorOpen lab
THE JAVASCRIPT FIELD GUIDE

Declaring & calling functions

Learn how JavaScript function declarations package reusable code, receive arguments through parameters, return values, and work with local and outer variables.

By the end you can
  • 01
    Declare and call a functionRead a function declaration, call it, and trace when the body actually runs.
  • 02
    Pass input and return outputDistinguish parameters from arguments and use return instead of only printing.
  • 03
    Reason about namesExplain local variables, outer variables, shadowing, and beginner-friendly function names.

Functions are reusable recipes

A function is a block of JavaScript you can give a name, then run whenever you need it. Instead of copying the same five lines into ten places, you write the steps once and call the function ten times.

You already met small helpers in Thinking like a developer: names like addTip and splitBill turn a messy problem into little jobs. This lesson gives those jobs their JavaScript shape: function declarations, parameters, arguments, return, and the variables that live inside or around a function.

The one-sentence definition

A function packages code behind a name, can receive input through parameters, and can hand one result back with return.

Real-life analogyA function is a recipe card

Writing the recipe card does not cook dinner. It stores the steps so a cook can use them later. A function declaration is the recipe card; a function call is the moment you ask the cook to make one serving.

In real life: Recipe title
In JavaScript: Function name, like makeGreeting
In real life: Blanks on the card: “___ cups of flour”
In JavaScript: Parameters in the declaration
In real life: What you put in today: “2 cups”
In JavaScript: Arguments in one call
In real life: The finished dish handed through the window
In JavaScript: The return value

Where the analogy stops: A recipe can be followed by a patient human who improvises. JavaScript follows the function exactly: misspell a name or forget a return value and you get the real language behavior, not a helpful guess.

Keep that separation in mind. New learners often stare at a function body and expect it to run from top to bottom immediately. It does not. The body waits until a call presses the button.

Function declarations

STEP THROUGH

A function declaration starts with the keyword function, then a name, parentheses, and a block:

Declaration shapePop out in the code editor (opens in a new tab)JavaScript
function nameHere(parameterHere) {  // steps go here  return valueHere;}

The name should tell readers what the block does. The parentheses hold input names. The braces hold the steps that run later. The return line is optional, but without it the call value is undefined.

The next replay is recorded from real functions in this lesson. First, predict what line 7 prints. Then step through the call. Notice that JavaScript does not step into lines 1–4 until line 6 calls the function.

Step through a function call
Step 0 of 6Ready
Your turn: follow the blue line

A function declaration is skipped until you call it. Change the name, predict the greeting, then step through the call and return.

Running in
  1. script
Next: line 6
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
function makeGreeting(name) {  const message = "Hello, " + name + "!";  return message;} console.log(greeting);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
Choose the argument passed on line 6.

Changing the argument starts a fresh replay with the same function body.

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

The important events are: line 6 calls makeGreeting, the parameter name receives the argument, line 2 creates a local message, line 3 returns it, and line 6 stores the returned value in greeting. Finally line 7 prints a value that was already returned.

Function declarations are hoisted

A function declaration can be called before its line in the same scope. That is called hoisting. This lesson uses the fact gently; the deeper Hoisting & the temporal dead zone lesson explains the full rule later.

A declaration can be called firstPop out in the code editor (opens in a new tab)JavaScript
sayHi(); function sayHi() {  return "Hi before the declaration line";}

Parameters vs arguments

INTERACTIVE

The two words are easy to mix up because they point at the same flow of data from different sides.

Parameter vs argument
WordWhere you see itPlain meaning
ParameterIn the declaration: function greet(name)The blank the function promises to fill for each call.
ArgumentIn the call: greet("Maya")The actual value supplied today.
Same counttotal(price, tax) and total(20, 3)Arguments line up with parameters from left to right.
Different countMissing or extra valuesMissing parameters become undefined; extra values are ignored here.

Try the calls below. The function has exactly two parameter names, but the call can provide zero, one, two, or three arguments. The output pane shows the actual local values JavaScript gives to the parameters.

Fill the parameter blanks
Function with two parametersPop out in the code editor (opens in a new tab)JavaScript
function describeOrder(item, quantity) {  return "item=" + item + ", quantity=" + quantity;} console.log(describeOrder());console.log(describeOrder("tea"));console.log(describeOrder("tea", 2));console.log(describeOrder("tea", 2, "ignored"));
Current call0 arguments
  • item parameterundefined
    quantity parameterundefined
    returned textitem=undefined, quantity=undefined
Try it yourself

No arguments means both parameters receive undefined. That is a real value meaning “nothing was provided”.

A real function call with different argument counts. Missing arguments become undefined; extra arguments do not create new parameters.

Missing arguments become undefined. Extra arguments are still passed to the call, but this function has no parameter name for the third one, so it does not use it. JavaScript has an older arguments object and modern rest parameters for collecting extras on purpose; those are the focus of the next lesson, Default & rest parameters.

Parameter or argument?
  • name in function greet(name) {}
  • "Maya" in greet("Maya")
  • price and tax in function total(price, tax)
  • 20, 3 in total(20, 3)
  • quantity in describeOrder(item, quantity)
  • The third value in describeOrder("tea", 2, true)
Try it yourself
0 of 6 correct

Sort each card by whether it names a blank in the declaration or a value in a call.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Returning values

INTERACTIVE

return is the kitchen window. The function has been doing work on its own counter; return hands one finished value back to whoever called it. Once that happens, the current call stops.

Returning is not the same as printing. console.log is a useful way to inspect a value, but it does not make that value reusable by the caller. A calculation helper should usually return the number it calculated, then the caller decides whether to print it, store it, or combine it with something else.

Returning is not the same as printing
Print, return, and stop earlyPop out in the code editor (opens in a new tab)JavaScript
let checks = 0; function logTotal(price, tax) {  const total = price + tax;  console.log(total);} function returnTotal(price, tax) {  const total = price + tax;  return total;} function isAllowed(age) {  checks = checks + 1;  if (age < 18) return false;  checks = checks + 1;  return true;}
Observed resultlog
  • console printed23
    call valueundefined
Try it yourself

logTotal prints 23, but its call value is undefined because it has no return statement. Printing is a side effect, not reusable output.

Three return experiments: no return gives undefined, return creates a reusable call value, and return stops the current call.
No return

The call value is undefined, even if the function printed something while it ran.

Return value

The call expression becomes the returned value, so other code can reuse it.

Early return

A return inside an if can end a call before later lines run.

You saw undefined in earlier lessons when the console had no useful expression value to show. Functions use the same value when no explicit return happens.

Local & outer variables

STEP THROUGH

Every function call gets its own little workspace. Parameters and variables declared inside the function are local to that call. Code outside cannot use those local names. A function can also read variables from the surrounding code; those are outer variables.

Real-life analogyLocal counter, shared pantry

A cook can use a private bowl while also reaching into a shared pantry. If the cook labels a bowl “pantry,” that label hides the shared shelf name while the cook is looking at the bowl.

In real life: Ingredients on the cook’s counter
In JavaScript: Local variables inside one call
In real life: Shared pantry shelves
In JavaScript: Outer variables nearby code can use
In real life: A bowl named pantry on the counter
In JavaScript: A local name shadowing an outer name
In real life: Changing the shared sugar jar
In JavaScript: Mutating an outer variable

Where the analogy stops: Real kitchens have people negotiating over shared shelves. JavaScript follows lexical lookup rules exactly; the deeper Scope & lexical environments lesson explains the full search path later.

Local names, outer names, and shadowing
Step 0 of 11Ready
Your turn: follow the blue line

Step through local variables, an outer variable, and a shadowed name. This is a guided replay of this example, not a debugger.

Running in
  1. script
Next: line 1
Click the blue line to take the next stepPop out in the code editor (opens in a new tab)JavaScript
let scoops = 0; function bake(label) {  const localBowl = label + " uses " + pantry;  {    let pantry = "cocoa";    scoops = scoops + 1;    return localBowl + "; local pantry is " + pantry;  }} const result = bake("cake");console.log(result);console.log(scoops);
CallStoreChangeResultRun = next line. Ran = already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.
Local, outer, and shadowed names
KindExampleWhat to remember
Localconst localBowl inside bakeCreated for this call; not visible outside the function.
Parameterlabel in function bake(label)Also local; it receives one argument for this call.
Outerlet scoops = 0 before the functionThe function can read or change it.
ShadowedA local pantry when an outer pantry existsThe inner name wins only in its local area.
Local variable outside the functionPop out in the code editor (opens in a new tab)JavaScript
function wrapGift() {  const ribbon = "blue";  return ribbon;} wrapGift();console.log(ribbon);

That last snippet throws a ReferenceError at line 7. The local ribbon existed while wrapGift was running, but outside code never gets a variable with that name. Return the value if outside code needs it.

Local or outer?
  • label inside function bake(label)
  • const localBowl declared inside bake
  • scoops declared before function bake
  • The first pantry declared before the function
  • let pantry = "cocoa" inside bake
  • result declared after calling bake
Try it yourself
0 of 6 correct

Sort each name by where it lives in the scope example.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Naming functions

STYLE

Functions do things, so their names usually start with verbs. The Coding style & naming lesson introduced this habit; functions make it especially valuable because a good name lets readers understand a call without opening the body.

Names that tell the job
GoalGood nameWhy it worksVague name
Calculate a cart totalcalculateTotalVerb + thing returnedtotal
Check an emailisValidEmailReads like a yes/no questioncheck
Fetch a saved usergetUserSays what value comes backdata
Format cents for displayformatPriceNames the transformationmakeNice

Prefer one job per function. If you are tempted to name something calculateAndPrintAndSaveTotal, split it. One function can calculate; another can print; a later lesson on callbacks shows how to pass functions around when jobs need to be connected.

Boolean-returning functions often read well with prefixes like is, has, or can: isValidEmail, hasDiscount, canCheckout. That makes if (canCheckout(cart)) read like a sentence.

Where you’ll use functions

Functions are everywhere once a program grows beyond a few lines. You use them to isolate repeated work, to name a small idea, and to test a piece of logic without clicking through the whole app.

Small helpers in a checkoutPop out in the code editor (opens in a new tab)JavaScript
function calculateSubtotal(items) {  return items.length * 10;} function addTip(total, rate) {  return total + total * rate;} function formatReceipt(name, total) {  return name + " owes $" + total;} const subtotal = calculateSubtotal(["tea", "cake"]);const withTip = addTip(subtotal, 0.2);console.log(formatReceipt("Maya", withTip));

Notice the data flow: values go in as arguments, each function returns one value, and the script stores the returned values for the next step. That pattern makes debugging easier. In the browser debugger you can step into a function to see its local variables, or step over it when the name already tells you enough.

Functions also appear as event handlers, validation helpers, formatters, and small calculations. Later in this module, Default & rest parameters, Function expressions & callbacks, Arrow functions, and Recursion add more ways to shape and use them.

Common misconceptions

WATCH OUT
The traps worth naming
  • “A declaration runs immediately.” It does not. A call runs the body.
  • “Parameters and arguments are the same word.” They describe different sides of the same handoff.
  • “Printing returns the value.” Printing is a side effect; return creates the call value.
  • “Every function returns something visible.” A function with no return gives undefined.
  • “A local variable exists after the call.” Local names disappear from outside code when the call ends.
  • “Same name means same variable.” A local name can shadow an outer name without changing it.

When output surprises you, read the call site first. What arguments went in? Did the function return a value? Did it change an outer variable? The Reading error messages and Debugging in the browser lessons are useful companions here because stack traces and step controls both show function calls.

Practice exercises

5 EXERCISES
Exercise 1 · Warm-upReturn the area of a rectangle

Write a function declaration named rectangleArea with two parameters, width and height. Return the area, then call it with 5 and 4.

Starter codePop out in the code editor (opens in a new tab)JavaScript
function rectangleArea(width, height) {
  // return the area
}

console.log(rectangleArea(5, 4));

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upPredict a function with no return

    Predict the exact value printed by the last line. Do not answer what result was inside the function; answer the call value.

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    function double(number) {
      const result = number * 2;
    }
    
    console.log(double(4));

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeFix the helper that logs instead of returns

      The starter code prints 10 and then NaN because tip is undefined. Change the helper so the final line prints 20.

      Starter codePop out in the code editor (opens in a new tab)JavaScript
      function getTip(total) {
        console.log(total * 0.2);
      }
      
      const tip = getTip(50);
      console.log(tip + 10);

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticePredict shadowing output

        Write the two console lines in order. This checks whether a local variable with the same name changes the outer one.

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        let label = "outer";
        
        function printLabel() {
          let label = "inner";
          console.log(label);
        }
        
        printLabel();
        console.log(label);

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeChoose a clear function name

          You need a function that receives cart items and returns the total price. Type a good function name. Several clear verb phrases are accepted.

          Answer, then press Check. Spacing and letter case don’t matter.

            Quiz

            8 QUESTIONS

            Read each snippet like a trace: declarations wait, calls fill parameters with arguments, return hands a value back, and local names stay inside their call.

            Lesson quiz · 8 questionsScore: first tries count
            1. Question 1 of 8What does a function declaration do when JavaScript first reaches it in this lesson’s examples?

              Choose an answer to see the explanation.

            2. Question 2 of 8What does the add(2, 5) call print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              function add(a, b) {
                return a + b;
              }
              console.log(add(2, 5));

              Choose an answer to see the explanation.

            3. Question 3 of 8In function greet(name) {} and greet("Ari"), which word is the argument?

              Choose an answer to see the explanation.

            4. Question 4 of 8What does the show("hi") example print last?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              function show(value) {
                console.log(value);
              }
              const result = show("hi");
              console.log(result);

              Choose an answer to see the explanation.

            5. Question 5 of 8What happens to code after return in the same function call?

              Choose an answer to see the explanation.

            6. Question 6 of 8What two lines does the shadowing example print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              let word = "outer";
              function sayWord() {
                let word = "inner";
                return word;
              }
              console.log(sayWord());
              console.log(word);

              Choose an answer to see the explanation.

            7. Question 7 of 8Which name best matches a function that checks whether an email string is valid?

              Choose an answer to see the explanation.

            8. Question 8 of 8What does the hoisted sayHi example print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(sayHi());
              function sayHi() {
                return "hi";
              }

              Choose an answer to see the explanation.

            Key takeaways

            • A function declaration names a reusable block; a call runs it.
            • Parameters are blanks in the declaration; arguments are values in a call.
            • return creates the call value and stops the current call.
            • Without an explicit return value, a function call gives undefined.
            • Local variables live inside a call; outer variables live around it and can be read or changed.
            • Good function names are clear verb phrases and usually describe one job.

            Final definition: a function is a named, reusable set of steps that can receive arguments, work with local variables, and return one value to its caller.

            Up next: Default & rest parameters.

            CompleteFrontend Clear concepts. Working examples.