Basics of working with user-defined functions in JavaScript

Now we will learn how to create our own functions, which can then be used like standard JavaScript functions and methods. Let's look at the syntax for creating our own function.

A function is created with the command function. Next, a space is followed by the name of the function, parentheses, and then curly brackets, in which some code is written:

javascriptfunction func() {
  // some code
}

Let's look at some example. Let's make a function called func, which, when invoked (called), will display an exclamation mark:

javascriptfunction func() {
  console.log('!');
}

Let's call our function now. To do this, write her name and parentheses:

javascriptfunction func() {
  console.log('!');
}

// Call our function:
func(); // shows '!'

You can call our function several times - in this case, each function call will show a new message:

javascriptfunction func() {
  console.log('!');
}

func(); // shows '!'
func(); // shows '!'
func(); // shows '!'

Functions can be invoked before they are defined:

javascriptfunc(); // shows '!'

function func() {
  console.log('!');
}
Задание 1

Make a function that prints your name to the console.

Задание 2

Make a function that prints the sum of numbers from 1 to 100 to the console.