What a function is
A function is a named container for a few lines of code that perform one task. Instead of writing everything in one long block, you split the program into small pieces, each with a name that says what it does. A function can then be run as many times as you need by using that name.
A function declaration in TypeScript looks like this:
function functionName() {
// function body: the instructions go here
}
It has two parts.
The header
- The
functionkeyword tells TypeScript that a function definition follows. functionNameis the name, an identifier you use to refer to the function. Names are case-sensitive, sosayHelloandsayhelloare different functions.- The parentheses
()hold the function's inputs. They are empty for now and are explained in later lessons.
The body
The body is the code that runs when the function is called. It is a sequence of statements between curly braces {}:
function sayHello() {
console.log("Hello World");
}
The line inside is indented to show that it belongs to the function. TypeScript doesn't require indentation, unlike Python, where it is part of the syntax. The curly braces mark where the body begins and ends, and indentation is just there to help human readers.
Declaring is not calling
If you run the function above on its own, nothing is printed. A declaration only tells TypeScript that the function exists. The body runs when you call the function, by writing its name followed by parentheses:
function sayHello() {
console.log("Hello World");
}
sayHello(); // Hello World
sayHello(); // Hello World
Every call runs the whole body again from the top, so two calls print the message twice. Forgetting the parentheses is a classic mistake: writing sayHello; refers to the function without running it, and nothing happens.
The return type
TypeScript lets you state what a function gives back. A function that only prints and returns nothing is annotated with : void, written after the parentheses:
function sayHello(): void {
console.log("Hello World");
}
The annotation is for the compiler and for readers. It is removed when the code is compiled to JavaScript. Without it, TypeScript works out the type itself, and in this case it would infer void anyway. At run time a function that returns nothing hands back undefined, so console.log(sayHello()) prints the greeting and then undefined.
The lesson's exercise
The task is to write a function named sayHello that prints Hello everybody!, and to call it. There are three steps:
- Declare the function at the top of the file, with the name
sayHelloand thevoidreturn type, because it prints a message and doesn't return a value. - Put the print statement in the body so every call shows the greeting. Statements end with a semicolon.
- Call
sayHello()below the function. Without this line the program would print nothing.
function sayHello(): void {
console.log("Hello everybody!");
}
sayHello();
This prints Hello everybody! once. A second sayHello(); line would print it twice.
Gotchas
- Missing braces. The body must be wrapped in
{ }. A header with no body, followed by a separate statement, makes the compiler report an error, and the function is not defined in the JavaScript it writes. - Calling before declaring. Function declarations are hoisted, so a call written above the declaration still works in the same file. Even so, defining functions before using them is easier to read.
- Different names. The name in the call has to match the name in the declaration exactly, including capitals.
Key takeaways
- A function groups statements under a name.
- The header is
function, a name and parentheses; the body is in curly braces. - Declaring a function does not run it; calling it with
name()does. - A function can be called as many times as needed.
: voidsays that the function returns nothing.