Skip to content
Exercise · Your First Program · lesson 7 of 7

Exercise: your first function

Write two functions: int square(int x) returns the square of x, and int sum3(int a, int b, int c) returns the sum of three numbers.

  • square(3) → 9
  • square(-4) → 16
  • sum3(1, 2, 3) → 6

Constraints: the numbers are any int values for which the result also fits in an int.

The "Run tests" button runs the checks from the first_test.cpp tab—you can read them but not change them. Each CHECK_EQ(call, expected) line calls your function and compares the result with the number on the right. The tests file pastes your code into itself with the line #include "first.cpp", so the program is built from one file and you don't need to declare the functions. The lesson counts as done after the "Submit for checking" button.

Do not add extra #include lines unless the task says so explicitly.

first.cpp — cpp
cpp · 8 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way