Introduction to Functions
Understand Introduction to Functions
Functions package a named operation so the same behavior can be called with different values. A function can accept inputs, compute a result, and return it to its caller.
What you will learn: the purpose of introduction to functions, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.
What you’ll learn
- Explain how introduction to functions works and where its type rules apply.
- Predict the exact output of the introduction to functions example.
- Diagnose a common issue: Declaring a function is not the same as calling it.
Build the mental model
TypeScript checks the kinds of arguments a function accepts and the values it returns. JavaScript performs the actual call at runtime. The declaration does not run its body; invoking it does. Think of a function as an input-to-output contract rather than a block copied into every call site.
Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.
See the relationship
Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.
Run the complete TypeScript example
This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.
TypeScript program
function double(value: number): number {
return value * 2;
}
console.log(double(6));
console.log(double(9));Verified output
12
18Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.
Trace every important step
First, double is declared. The call double(6) binds value to 6 and returns 12. The next call binds a fresh parameter value of 9 and returns 18. Each call runs the body independently.
Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.
Avoid these common mistakes
Declaring a function is not the same as calling it. The declared parameter type number does not convert a runtime string into a number.
How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.
Practice and experiment
Add a triple(value: number) function and call both functions with 4. Predict both results before running.
Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.
Check your understanding
Review the key idea
Introduction to Functions: Functions package a named operation so the same behavior can be called with different values.
First, double is declared. The call double(6) binds value to 6 and returns 12. The next call binds a fresh parameter value of 9 and returns 18. Each call runs the body independently.
Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.
Reaching the recap and completing the quizzes are separate milestones.