Function definition in C++

Level 396 keystrokes

Defines add, which takes two int parameters and returns their sum, then calls it twice and prints 8 and 30.

C++ code

// The int before the name is the return type; a and b are int parameters
1int add(int a, int b) {
// return sends the value of a + b back to the caller
2 return a + b;
3}
4
// add(3, 5) returns 8, which cout prints; endl ends the line
5cout << add(3, 5) << endl;
// Calls the same function with other arguments and prints 30
6cout << add(10, 20) << endl;

Output

8
30
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 8
  • ,× 3
  • ;× 3
  • (× 3
  • )× 3
  • {× 1
  • }× 1
  • +× 1

More C++ examples at level 3

← C++ typing practice