# Functions A function is a named code block that performs a specific task. ## Declaring a Function Declaring a Function The syntax to declare a function is: ```javascript= function nameOfFunction () { // function body } ``` - A function is declared using the `function` keyword. - The basic rules of naming a function are similar to naming a variable. It is better to write a descriptive name for your function. For example, if a function is used to add two numbers, you could name the function add or addNumbers. - The body of function is written within {}. For example, ```javascript! // declaring a function named greet() function greet() { console.log("Hello there"); } ``` ## Calling a Functions In the above program, we have declared a function named greet(). To use that function, we need to call it. Here's how you can call the above greet() function. ```javascript! // function declaration function greet() { console.log("Hello there"); } // function call greet(); ``` ### Demo Code: Declaring & Calling Functions WITHOUT Paremeters and/or Return Statements ```javascript! // Write a program that performs addition, subtraction, division, multiplication of two numbers. The program should first ask the user to choose one of the four operations. Next, the program should perform the operation by asking the user to enter two numbers and performing the operation with those two numbers // PROGRAM ALGORITHM // Functions: // a. Function addTwoNums(): // i. Prompts the user to enter two numbers and displays the sum. // b. Function subTwoNums(): // i. Prompts the user to enter two numbers and displays the difference. // c. Function multTwoNums(): // i. Prompts the user to enter two numbers and displays the product. // d. Function divTwoNums(): // i. Prompts the user to enter two numbers and displays the quotient. // Program // 1. Display a menu of operations for the user to choose from (addition, subtraction, multiplication, division). // 2. Prompt the user to enter their choice of operation. // 3. Performing the selected operation: // a. If the choice is "add," call addTwoNums(). // b. If the choice is "subtract," call subTwoNums(). // c. If the choice is "multiply," call multTwoNums(). // d. If the choice is "divide," call divTwoNums(). // 4. End the program. // PROGRAM CODE // Function to add two numbers function addTwoNums() { let num1 = parseInt(prompt("Enter the first number: ")) let num2 = parseInt(prompt("Enter the second number: ")) console.log(`${num1} + ${num2} = ${num1 + num2}`) } // Function to subtract two numbers function subTwoNums() { let num1 = parseInt(prompt("Enter the first number: ")) let num2 = parseInt(prompt("Enter the second number: ")) console.log(`${num1} - ${num2} = ${num1 - num2}`) } // Function to multiply two numbers function multTwoNums() { let num1 = parseInt(prompt("Enter the first number: ")) let num2 = parseInt(prompt("Enter the second number: ")) console.log(`${num1} * ${num2} = ${num1 * num2}`) } // Function to divide two numbers function divTwoNums() { let num1 = parseInt(prompt("Enter the first number: ")) let num2 = parseInt(prompt("Enter the second number: ")) console.log(`${num1} / ${num2} = ${num1 / num2}`) } // Prompting the user for operation choice let userChoice = prompt("Enter the operation: ") // Performing the selected operation if (userChoice === "add") { // Call the addition function addTwoNums(); } else if (userChoice === "subtract") { // Call the subtraction function subTwoNums(); } else if (userChoice === "multiply") { // Call the multiplication function multTwoNums(); } else if (userChoice === "divide") { // Call the division function divTwoNums(); } ``` ### Demo Code: Declaring & Calling Functions WITH Paremeters and/or Return Statements ```javascript! // PROGRAM DESCRIPTION // Write a program that performs addition, subtraction, division, multiplication of two numbers. The program should first ask the user to choose one of the four operations. Next, the program should perform the operation by asking the user to enter two numbers and performing the operation with those two numbers // PROGRAM ALGORITHM // Functions: // a. Function addTwoNums(): // i. Prompts the user to enter two numbers and RETURNS the sum. // b. Function subTwoNums(): // i. Prompts the user to enter two numbers and RETURNS the difference. // c. Function multTwoNums(): // i. Prompts the user to enter two numbers and RETURNS the product. // d. Function divTwoNums(): // i. Prompts the user to enter two numbers and RETURNS the quotient. // Program // 1. Display a menu of operations for the user to choose from (addition, subtraction, multiplication, division). // 2. Prompt the user to enter their choice of operation. // 3. Performing the selected operation: // a. If the choice is "add," call addTwoNums(). // b. If the choice is "subtract," call subTwoNums(). // c. If the choice is "multiply," call multTwoNums(). // d. If the choice is "divide," call divTwoNums(). // 4. End the program. // PROGRAM CODE // Function to add two numbers and return the result function addTwoNums(num1, num2) { return num1 + num2; } // Function to subtract two numbers and return the result function subTwoNums(num1, num2) { return num1 - num2; } // Function to multiply two numbers and return the result function multTwoNums(num1, num2) { return num1 * num2; } // Function to divide two numbers and return the result function divTwoNums(num1, num2) { if (num2 !== 0) { return num1 / num2; } else { return "Cannot divide by zero."; } } // Display menu options console.log("Select an operation:"); console.log("1. Addition"); console.log("2. Subtraction"); console.log("3. Multiplication"); console.log("4. Division"); // Prompt the user to enter their choice let userChoice = prompt("Enter the number corresponding to the operation: "); // Prompt the user to enter two numbers let num1 = parseInt(prompt("Enter the first number: ")); let num2 = parseInt(prompt("Enter the second number: ")); // Perform the selected operation and display the result if (userChoice === "1") { console.log(`${num1} + ${num2} = ${addTwoNums(num1, num2)}`); } else if (userChoice === "2") { console.log(`${num1} - ${num2} = ${subTwoNums(num1, num2)}`); } else if (userChoice === "3") { console.log(`${num1} * ${num2} = ${multTwoNums(num1, num2)}`); } else if (userChoice === "4") { console.log(`${num1} / ${num2} = ${divTwoNums(num1, num2)}`); } else { console.log("Invalid choice. Please enter a number between 1 and 4."); } ``` ### Summary Video: [YouTube](https://www.youtube.com/watch?v=t_fCi2spRz0)