0
0
Javascriptprogramming~15 mins

Function declaration in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Function declaration
πŸ“– Scenario: You are creating a simple calculator that adds two numbers. To do this, you will write a function that takes two numbers and returns their sum.
🎯 Goal: Build a JavaScript function called addNumbers that takes two parameters and returns their sum. Then call this function with specific numbers and display the result.
πŸ“‹ What You'll Learn
Declare a function named addNumbers with two parameters: a and b
Inside the function, return the sum of a and b
Call the function addNumbers with the arguments 5 and 7
Print the result of the function call using console.log
πŸ’‘ Why This Matters
🌍 Real World
Functions help organize code into reusable blocks, like a recipe you can use many times. This is useful in calculators, games, websites, and more.
πŸ’Ό Career
Knowing how to write and use functions is a basic skill for any programming job. It helps you write clean, easy-to-understand, and reusable code.
Progress0 / 4 steps
1
Create the function declaration
Write a function declaration named addNumbers that takes two parameters called a and b. Inside the function, return the sum of a and b.
Javascript
Need a hint?

Use the function keyword followed by the function name and parentheses with parameters. Use return to send back the sum.

2
Call the function with arguments
Call the function addNumbers with the arguments 5 and 7 and store the result in a variable called result.
Javascript
Need a hint?

Use const result = addNumbers(5, 7); to call the function and save the answer.

3
Print the result
Use console.log to print the value of the variable result.
Javascript
Need a hint?

Use console.log(result); to show the sum in the console.

4
Run and check the output
Run the program and check that the output printed is 12.
Javascript
Need a hint?

The console should show the number 12 because 5 + 7 = 12.