Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Parameters and Return Statement | Functions
Introduction to TypeScript
course content

Course Content

Introduction to TypeScript

Introduction to TypeScript

1. TypeScript Fundamentals
2. Conditional Statements
3. Arrays
4. Loops
5. Functions

bookParameters and Return Statement

Continuing our work and exploration of functions. In this chapter, we will delve into a fundamental feature of functions: accepting parameters and returning data. This allows us to turn any function into a calculation machine. Let's start with something basic - the code below will take 2 numbers as parameters and return their sum:

123456
function add(a: number, b: number): number { return a + b; } let result = add(5, 10); console.log(result);
copy

In the example above, inside the parentheses, we specified 2 parameters that the function will accept, along with the data type of these parameters.

What are the parameters?

These are the data that we will provide when calling the function, and we can use these parameters within the body of the function. For example, in the above example, when we call the function, we provide two numbers, and inside the function's body, these two numbers are added together.

After all of this, the function returns the final value.

How does the function return a value?

Through the keyword return. You can see that we return the sum of these two parameters using return.

We also specify the data type returned through a colon when declaring the function.

Too much text, let's look at another example of declaring and using a function:

1234567891011121314
function sumArray(numbers: number[]): number { let sum = 0; for (let i = 0; i < numbers.length; i++) { sum += numbers[i]; } return sum; } let numbers = [1, 2, 3, 4, 5]; let total = sumArray(numbers); console.log(`Sum of numbers in the first array: ${total}`); let numbers_2 = [2, 4, 6, 8, 10]; let total_2 = sumArray(numbers_2); console.log(`Sum of numbers in the second array: ${total_2}`)
copy

The function above calculates the sum of all the elements in the array, starting from the first and ending with the last. Here you can see that inside the function's body, there is a loop that performs this operation. This way, we have reduced a significant number of lines of code by using the function as a reusable block of code. We call this block of code several times with just one line of code.

Notice that we initialize a variable using the function. This variable will be the same type as the function's return type.

1. What is the purpose of parameters in a TypeScript function?
2. What is the primary role of return values in TypeScript functions?
What is the purpose of parameters in a TypeScript function?

What is the purpose of parameters in a TypeScript function?

Select the correct answer

What is the primary role of return values in TypeScript functions?

What is the primary role of return values in TypeScript functions?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 2
some-alt