Function Fundamentals
Parameters & Arguments
Return Values
Pass by Value vs Ref
Iteration in Functions
100

What is the benefit of using a function in your program?

Reduces repetition, improves readability, simplifies debugging.

100

What's the difference between a parameter and an argument?

Parameter = placeholder; Argument = actual value passed.

100

What keyword sends data back from a function?

return

100

T/F: Pass by value allows changing the original variable.

False

100

Which loop repeats a known number of times?

for loop

200

Name the 3 components of a function definition.

Return type, function name, parameter list.

200

Define a function that accepts a string.

void greet(string name){}

200

Complete this: int square(int x) { _____ }


return x * x;

200

Convert: void changeNum(int num) → pass by reference.

void changeNum(int &num)

200

Write countdown(int n) that prints from n to 1.

void countdown(int n) {

  for (int i = n; i >= 1; --i)

    cout << i << " ";

}

300

Predict the output of calling a function twice.

Function executes twice; prints twice (e.g., Hello!Hello!).

300

What’s the output of a function that modifies a value passed by value?

Original variable unchanged due to pass-by-value.

300

Why is it bad to print and return in the same function?

Mixing responsibilities — should separate logic from output.

300

Output of modifying a variable passed by reference?

Value updated and printed (e.g., 15).

300

What is printed: loop from 0 to n in steps of 2?

Output is 0 2 4

400

Write a function that prints "SI Rocks!" 5 times using a for loop.

void printMessage() {

  for (int i = 0; i < 5; ++i) cout << "SI Rocks!\n";

}

400

How would you create a function using 'int x' to not modify the actual value of x?

Change to pass-by-reference using int &x.

400

Compare a void and value-returning function.

Void = performs action, no return. Value-returning = sends result back.

400

What's the risk of reference? How to prevent it?

Risk = accidental data change; prevent with const & for read-only.

400

Write countdown(int n) that it returns the sum, not print.

int countdownSum(int n) {

  int total = 0;

  for (int i = n; i >= 1; --i)

    total += i;

  return total;

}

500

Write a selection sort function that sorts the vector: 2,1,6,4,3

 Min and Swap function.

500

Write a function to swap two integers using reference. Why reference?

void swapVals(int &a, int &b) {

  int temp = a;

  a = b;

  b = temp;

}

500

Write a function to return the sum of 1 through n.

int sumToN(int n) {

  int sum = 0;

  for (int i = 1; i <= n; ++i) sum += i;

  return sum;

500

Write a function to average two values and store in a reference variable.

void average(int a, int b, double &avg) {

  avg = (a + b) / 2.0;

}

500

Write drawRow(int length) to print a row of *

void drawRow(int length) {

  for (int i = 0; i < length; ++i)

    cout << "*";

  cout << endl;

}

M
e
n
u