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

Course Content

Introduction to TypeScript

Parameters and Return StatementParameters 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:

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:

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?

Select the correct answer

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

Select the correct answer

Everything was clear?

Section 5. Chapter 2
course content

Course Content

Introduction to TypeScript

Parameters and Return StatementParameters 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:

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:

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?

Select the correct answer

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

Select the correct answer

Everything was clear?

Section 5. Chapter 2
some-alt