Зміст курсу
Introduction to TypeScript
Introduction to TypeScript
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:
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:
function 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:
function 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!
Дякуємо за ваш відгук!