Warm Up · Function, if-else
Sum of Two Integers
Problem Statement
Write a program that defines a function to calculate the sum of two integers and prints the result. Call this function by passing two integer values.
Example
Input: 5, 3
Process: a + b => 5 + 3 = 8
Output: 8
Approach
- Define a function that takes two numbers as input.
- Add the two numbers inside the function.
- Call the function with two integers & print the result.
Visualisation
1Define a function
function Sum(a, b)2Add the numbers
return a + b;3Call the function
Sum(5, 3);4Print the result
console.log(result);Explanation
Sum(a, b)is a function that takes two arguments.- Adds them and stores the result in a variable named
add. - Prints the result.
Sum(a, b)calls the function with a=5 & b=3, so it prints 8.
function sum(a, b) {
let add = a + b;
console.log(add);
}
sum(5, 3);