6.x — Chapter 6 summary and quiz

📘 Chapter 6⏱ about 2 min read✍️ Beginner friendly
Read one box at a time. No rush.

Quick review#

  • A function is a named group of steps. Call it with name();.
  • The return type says what comes back. void = nothing.
  • Parameters are inputs. By default the function gets copies.
  • Variables live only inside their { } block (scope).
  • Declare a function before you call it.
  • Overloading: same name, different parameters.
  • Headers (.h) declare, source files (.cpp) define.
Quick check

Pick an answer. You find out right away if it is right.

Quick check #1

What does a function with return type void give back?

Quick check #2

By default, what does a function get when you pass it an int?

Quick check #3

Where should function declarations usually go when splitting code into files?

Quiz time
Question #1

What is wrong here?

C++
int doubleIt(int x){    int result { x * 2 };}

The function says it returns an int, but it never returns anything. Add return result; at the end.

Question #2

What does this print?

C++▶ Run
void change(int n){    n = n * 10;} int main(){    int x { 3 };    change(x);    std::cout << x;}

3. The function changes a copy of x, not x itself.

Question #3

Write a function isEven that takes an int and returns true if it is even.

C++
bool isEven(int n){    return n % 2 == 0;}
Question #4

Write a function getNumber() that asks the user for a number and returns it. Then use it in main to read two numbers and print their sum.

C++▶ Run
#include <iostream> int getNumber(){    std::cout << "Enter a number: ";    int n {};    std::cin >> n;    return n;} int main(){    int a { getNumber() };    int b { getNumber() };    std::cout << "Sum: " << a + b << '\n';    return 0;}

See how getNumber saved us from writing the same three lines twice?

Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.