Functions introduction
Functions are what TypeScript is made of. When you write large advanced programs or applications, it's advisable to organize all your code into functions. Let's look at what a function is and its syntax:
function greeting() {
console.log("Hello there!");
}
To declare a function, you just need to write function
, followed by the function name and parentheses ()
. Then, you open curly braces, which will contain the function body. Inside the function body, you specify what your function will do. In the example above, the function prints a greeting to the console. Let's call the function and see how it works:
1234function greeting() { console.log("Hello there!"); } greeting()
To call a function, all we need to do is write it in the code, and the function will do its job. We can call it many times in a row or even within loops, for example:
1234567function greeting() { console.log("Hello there!"); } for (let i = 0; i < 3; i++) { greeting() }
Instead of writing code each time to display a message in the console, we simply call the greeting function.
Of course, this is the simplest example of a function. Functions have virtually limitless capabilities. You can encapsulate complex algorithms within a single function. You can use functions within functions and much more. In this section, we'll explore the fundamental techniques of working with functions, including passing parameters and returning values from functions. This is where real programming begins!
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 2.94
Functions introduction
Sveip for å vise menyen
Functions are what TypeScript is made of. When you write large advanced programs or applications, it's advisable to organize all your code into functions. Let's look at what a function is and its syntax:
function greeting() {
console.log("Hello there!");
}
To declare a function, you just need to write function
, followed by the function name and parentheses ()
. Then, you open curly braces, which will contain the function body. Inside the function body, you specify what your function will do. In the example above, the function prints a greeting to the console. Let's call the function and see how it works:
1234function greeting() { console.log("Hello there!"); } greeting()
To call a function, all we need to do is write it in the code, and the function will do its job. We can call it many times in a row or even within loops, for example:
1234567function greeting() { console.log("Hello there!"); } for (let i = 0; i < 3; i++) { greeting() }
Instead of writing code each time to display a message in the console, we simply call the greeting function.
Of course, this is the simplest example of a function. Functions have virtually limitless capabilities. You can encapsulate complex algorithms within a single function. You can use functions within functions and much more. In this section, we'll explore the fundamental techniques of working with functions, including passing parameters and returning values from functions. This is where real programming begins!
Takk for tilbakemeldingene dine!