Without notes, state yesterday’s main idea and one unresolved question.
Functions and parameters
JavaScript Foundations I
Objective
Encapsulate one responsibility into reusable operations.
Variables are measured signals, functions are reusable circuit blocks, and control flow determines which path becomes active.
- declaration and invocation
- parameters and return values
- pure functions
Why this matters
Yesterday you wrote your first function because the lab needed one. Today you learn what they actually are, and it is the most important idea in the week: a function is a named piece of work you can run again with different inputs.
Without functions, calculating the parallel resistance of three different pairs means writing the formula three times — and mistyping it once. With functions you write it once, name it, and call it. By the end of the hour you will have three calculation functions you can trust and reuse for the rest of this course.
Declaring and calling
function seriesResistance(r1, r2) {
return r1 + r2;
}
console.log(seriesResistance(220, 330));
550
Piece by piece:
function— the keyword that declares a function. Declaring defines it; it does not run it.seriesResistance— the name. Name functions for what they produce or do.(r1, r2)— the parameters: placeholder names for values supplied later.{ ... }— the body, the code that runs on each call.return r1 + r2— sends a value back out and ends the function immediately.
seriesResistance(220, 330) is the call (or invocation). The values 220 and 330 are
the arguments. Parameters are the names in the declaration; arguments are the real values at
the call. Inside the body r1 is 220 and r2 is 330, and the call becomes 550 wherever
it appears.
A function is a reusable circuit block
A function is an op-amp block on a schematic: defined once with its inputs and one output, dropped in wherever you need that behaviour. Parameters are the input pins, the return value is the output pin, and the body is what is inside the package. You do not redraw the internals at every location — you place the block and wire it up.
Returning versus logging
This is where most beginners lose a day, so read it slowly.
console.log shows a value to a human. return hands a value back to the code that made
the call. They are unrelated.
function logSeries(r1, r2) {
console.log(r1 + r2);
}
const shown = logSeries(220, 330);
console.log(shown);
550
undefined
The 550 was printed by the function, but shown is undefined, because logSeries never
returned anything. A function with no return returns undefined. That value is useless to
the rest of the program — you cannot double it, compare it, or put it in a report.
Now the returning version:
const doubled = seriesResistance(220, 330) * 2;
console.log(doubled);
1100
The call produced a real number, so arithmetic worked on it. Return a value so callers can use the result. Logging is for you; returning is for the program.
The technician and the noticeboard
A technician who returns a measurement hands you a slip you can file, total, or act on. A technician who logs it shouts the number across the workshop. You heard it; you are not holding anything.
Tutor mode — before your lab, if this still feels blurry
"Explain the difference between logging a value and returning a value from a function."
Ask for the explanation, one small example, and then a question to answer yourself. Do not ask
for the finished lab file — the point is that you can predict the undefined above without
running it.
Parameters make one function serve many cases
Change the arguments and the same function answers a different question:
function parallelResistance(r1, r2) {
return (r1 * r2) / (r1 + r2);
}
console.log(parallelResistance(220, 330));
console.log(parallelResistance(1000, 1000));
132
500
Sanity-check both: two equal resistors in parallel give half of one, so 1000 and 1000 giving 500 is right. That is the Day 16 habit — verify the formula by hand once, then trust the function.
The parentheses in (r1 * r2) / (r1 + r2) are not optional. Without them, precedence would
divide r2 by r1 first and give a wrong answer.
Pure functions
A pure function has two properties:
- Given the same arguments it always returns the same result.
- It changes nothing outside itself — no printing, no editing variables declared elsewhere.
seriesResistance and parallelResistance are pure. logSeries is not: printing is a change to
the outside world, called a side effect.
Pure functions are worth aiming for because they are trivially testable. You call it with known inputs, compare the returned value with the answer you worked out by hand, and you are done — no setup, no state, no reading the terminal.
The practical rule for today: keep calculation separate from input and output. Calculation functions return numbers. A separate part of the program prints them.
function wattage(volts, amps) {
return volts * amps;
}
console.log(`Power: ${wattage(9, 0.02)} W`);
Power: 0.18 W
wattage knows nothing about printing. Later, when the same number must go into a web page
instead of a terminal, the function does not change at all.
One minute
Add console.log("calculating") inside parallelResistance, then call it three times. You now
get three stray lines and no way to use the function quietly. Delete it — that is impurity
costing you something concrete.
Arrow functions
There is a shorter syntax you will see constantly:
const parallel = (r1, r2) => (r1 * r2) / (r1 + r2);
console.log(parallel(220, 330));
132
An arrow function is written as parameters, then =>, then the body. When the body is a
single expression with no braces, its value is returned automatically — there is no return
keyword. It is stored in a variable like any other value, and called the same way.
Both forms are correct. Use function while you are learning, because the explicit return is
visible; recognise arrows because everyone else's code is full of them.
Walkthrough
Create resistors.js with all three functions and no printing inside any of them.
function seriesResistance(r1, r2) {
return r1 + r2;
}
function parallelResistance(r1, r2) {
return (r1 * r2) / (r1 + r2);
}
function wattage(volts, amps) {
return volts * amps;
}
console.log("Series 220+330: ", seriesResistance(220, 330));
console.log("Parallel 220|330:", parallelResistance(220, 330).toFixed(2));
console.log("Parallel 1k|1k: ", parallelResistance(1000, 1000));
console.log("Wattage 9V 20mA: ", wattage(9, 0.02));
Run node resistors.js:
Series 220+330: 550
Parallel 220|330: 132.00
Parallel 1k|1k: 500
Wattage 9V 20mA: 0.18
Every console.log is outside the functions. The functions compute; the script reports.
Checkpoint
Say which of the four printed lines could be fed into another calculation unchanged. (Three —
.toFixed(2) turned the second into a string, which is fine for display and wrong as input.)
Your turn
Deliverable: three tested calculation functions with explicit returns.
- In
js-basics/, createresistors.jsand writeseriesResistance,parallelResistance, andwattageyourself. Every one ends inreturn. Noconsole.loginside any of them. - Work out four expected answers on paper first: series of 100 and 100; parallel of 100 and 100; parallel of 220 and 330; wattage at 12 V and 0.5 A.
- Call each function and log the result next to the answer you predicted, like
console.log("expected 200, got", seriesResistance(100, 100));. - Any mismatch is a bug in the code or in your arithmetic. Find out which before moving on.
- Add a
report(label, value)function that only prints, using a template literal. Use it for all your output. Calculation and printing are now in different functions. - Prove the separation: call
parallelResistanceinside aseriesResistancecall — for exampleseriesResistance(470, parallelResistance(220, 330))— and log the result. Nothing prints from inside; values simply flow. - Convert one function to an arrow function. Confirm the output is identical.
Common pitfalls
- Logging instead of returning. The number appears on screen and the caller gets
undefined. If you want to use a result,returnit. - Declaring but never calling. A declaration alone runs nothing. You need the parentheses.
- Wrong argument order.
wattage(0.02, 9)gives the same answer here, butdivide(volts, ohms)called backwards does not. Order is part of the contract. - Printing inside a calculation function. It works today and blocks reuse tomorrow. Keep calculation pure.
Verify it yourself
Open today's reference, MDN's Dynamic scripting with JavaScript, and find its pages on functions and on return values.
- MDN describes default parameters — a parameter with a fallback value. Find the syntax and
write down when it would help in
wattage. - Does MDN agree that a function with no
returnstatement evaluates toundefined? Find the sentence.
Write both answers as comments at the bottom of resistors.js.
The hour
- 0–5 min Recall
- 5–20 min Learn
Read only the listed concept notes and official reference sections needed today.
- 20–48 min Build
Create functions for series resistance, parallel resistance, and wattage. Keep input/output separate from calculation.
- 48–55 min Explain and verify
Run the result, inspect evidence, and explain the data/control flow in your own words.
- 55–60 min Quiz and commit
Complete the quiz, record one lesson, and commit the verified change when applicable.
What to hand in
Three tested calculation functions with explicit returns.
Working with AI today
Ask for explanations, analogies, questions, and hints. Do not request a complete finished solution first.
Explain the difference between logging a value and returning a value from a function.
References
End-of-day quiz
Explain-back gate
Pass the quiz above to unlock completion.
Quiz + explain-back checks required.