Codeloft
0 0 XP

Functions

8 min 20 XP + 10 for no hints

A function is a named block of code you can run again and again. In C++ you write its return type, its name and its parameters: int square(int n) { return n * n; }

void means a function returns nothing; it just does something, like printing.

C++ reads your file from top to bottom, so write a function above main before you call it. Open Step through to see how a call jumps into the function and comes back with the answer.

Example
int add(int a, int b) {
    return a + b;
}

int main() {
    std::cout << add(2, 3) << std::endl;
    return 0;
}
Output
5
The return type before the name must match what you return: an int function returns an int.

Your tasks

0 of 3 done
  1. Define int square(int n)
  2. Return the number multiplied by itself
  3. Define square above main

Quick check

What does void mean in void greet()?

square.cppChecked as you type
No problems found. Unclosed tags, missing semicolons and errors show up here in plain words.