TypeScript Tutorial
TypeScript Functions
A function has a return type, a name, and a body. Call it to reuse work without copy-paste.
A named piece of work
A function packages a block of statements behind a name. You write the steps once. You call the name whenever you need those steps. TypeScript checks the argument types and the value you return.
Every function has a name, parentheses, a brace body, and a return type. The return type is what the function hands back, or void if it hands back nothing. There is no main. The file runs from the top. Functions run when something calls them.
Return a value
Write the return type after the parameter list: function square(x: number): number. The type after the colon must match what return sends back. The argument at the call site becomes the parameter inside the function.
Example
function square(x: number): number {
return x * x;
}
console.log(square(3));
const area: number = square(5);
console.log(area);square(3) is a call. The argument 3 becomes the parameter x. Whenreturn runs, the call is replaced by 9. You can print that value or store it.
Click Try it in TypeScript under the example. That opens /typescript/try, a type-check-and-run editor.
void means no value back
A void function does work — usually printing — and then stops. There is no value to assign. You can write a bare return; to leave early. You cannot write const n: number = greet("Mina");.
Example
function greet(name: string): void {
console.log("hello, " + name);
}
greet("Mina");
greet("Omar");Two calls, one definition. Change the message in greet and every call site updates. That is the whole reason to extract a function: one place to fix, not six pasted copies.
Annotate parameters and the return
TypeScript will not guess a safe type if you leave the annotations off under strict. Name the type of each parameter. Name the return type after the closing parenthesis. If the body has more than onereturn, every path must send back the same kind of value.
Example
function maxOf(a: number, b: number): number {
if (a > b) {
return a;
}
return b;
}
console.log(maxOf(4, 9));
console.log(maxOf(12, 3));The compiler already knows maxOf exists when the calls run. Function declarations are hoisted: you may call them above or below the definition in the same file. Keep helpers first anyway. The file stays easier to read.
The return type is required here
This tutorial writes the return type on every function. void is the type that means “returns nothing”. A missing annotation can slip through in loose compiler settings. On StudyGrid the editor runstsc with strict, so annotate.
: number— the function returns a number.: string— the function returns text.: boolean— the function returns true or false.: void— the function returns nothing you can store.
If you promise : number and then return "9", tsc reports an error and the program does not run. Fix the return, or fix the annotation.
What belongs in a function
Give a function one job and a name that says that job: square, greet,maxOf. If you cannot name it without the word “and”, it is probably two functions. Leave program setup at the top of the file and factor the repeated pieces.
Example
function square(n: number): number {
return n * n;
}
function cube(n: number): number {
return n * n * n;
}
for (let i: number = 1; i <= 4; i++) {
console.log(i + " squared " + square(i) + ", cubed " + cube(i));
}Next: how arguments get into the function — required, optional, and default values.