Back to Interview Prep

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 functionfunction Sum(a, b)
2Add the numbersreturn a + b;
3Call the functionSum(5, 3);
4Print the resultconsole.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);
This is a free preview lesson. Sign up to unlock the full course.Sign up freeSign in