cppbeginner10 minutes

Build a Function to Calculate the Area of a Rectangle

Write a simple C++ function that calculates the area of a rectangle given its width and height.

Challenge prompt

Create a function named calculateArea that takes two integers, width and height, and returns the area of the rectangle (width * height). The function should return an integer representing the area.

Guidance

  • Define a function with the name calculateArea that accepts two integer parameters.
  • Use the return statement to send back the product of width and height.
  • Make sure your function has the correct return type.

Hints

  • The area of a rectangle is width multiplied by height.
  • The function signature should look like: int calculateArea(int width, int height).

Starter code

int calculateArea(int width, int height) {
    // Your code here
}

Expected output

calculateArea(5, 10) should return 50 calculateArea(3, 7) should return 21

Core concepts

functionsreturn statement

Challenge a Friend

Send this duel to someone else and see if they can solve it.