Bird
Raised Fist0
C++programming~20 mins

Function parameters in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Function Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of function with default parameters
What is the output of this C++ code?
C++
#include <iostream>
#include <string>
using namespace std;

void greet(string name = "Guest", int times = 1) {
    for (int i = 0; i < times; i++) {
        cout << "Hello, " << name << "!\n";
    }
}

int main() {
    greet();
    greet("Alice", 2);
    return 0;
}
A
Hello, Guest!
Hello, Guest!
Hello, Alice!
Hello, Alice!
B
Hello, Guest!
Hello, Alice!
Hello, Alice!
C
Hello, Guest!
Hello, Alice!
D
Hello, Alice!
Hello, Alice!
Attempts:
2 left
💡 Hint
Remember how default parameters work when no arguments are passed.
❓ Predict Output
intermediate
2:00remaining
Output of function with pass-by-reference parameter
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void addOne(int &num) {
    num += 1;
}

int main() {
    int a = 5;
    addOne(a);
    cout << a << endl;
    return 0;
}
A
5
B
Compilation error
C
6
D
Undefined behavior
Attempts:
2 left
💡 Hint
Think about how passing by reference affects the original variable.
❓ Predict Output
advanced
2:00remaining
Output of function with const reference parameter
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void printValue(const int &val) {
    cout << val << endl;
}

int main() {
    int x = 10;
    printValue(x);
    return 0;
}
A
10
B
Compilation error
C
0
D
Undefined behavior
Attempts:
2 left
💡 Hint
Const references allow reading but not modifying the argument.
❓ Predict Output
advanced
2:00remaining
Output of function with pointer parameter
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void setZero(int *ptr) {
    if (ptr) {
        *ptr = 0;
    }
}

int main() {
    int a = 5;
    setZero(&a);
    cout << a << endl;
    return 0;
}
A
0
B
5
C
Compilation error
D
Segmentation fault
Attempts:
2 left
💡 Hint
Pointers can be used to modify the value they point to.
❓ Predict Output
expert
2:00remaining
Output of overloaded functions with different parameter types
What is the output of this C++ code?
C++
#include <iostream>
using namespace std;

void print(int x) {
    cout << "int: " << x << endl;
}

void print(double x) {
    cout << "double: " << x << endl;
}

int main() {
    print(5);
    print(5.0);
    print('5');
    return 0;
}
A
int: 5
double: 5.0
int: 53
B
int: 5
double: 5
int: 5
C
int: 5
double: 5
Compilation error
D
int: 5
double: 5
int: 53
Attempts:
2 left
💡 Hint
Remember how char is promoted to int in function overloading.

Practice

(1/5)
1.

What is the purpose of function parameters in C++?

easy
A. To send data into functions so they can use it
B. To define the return type of a function
C. To declare variables inside the function body
D. To specify the function's name

Solution

  1. Step 1: Understand function parameters

    Function parameters are variables listed in the function definition that receive values when the function is called.
  2. Step 2: Identify their purpose

    They allow the function to work with different data each time it is called, making the function flexible and reusable.
  3. Final Answer:

    To send data into functions so they can use it -> Option A
  4. Quick Check:

    Function parameters = input data for functions [OK]
Hint: Parameters pass data into functions for reuse [OK]
Common Mistakes:
  • Confusing parameters with return types
  • Thinking parameters declare variables inside function body
  • Believing parameters define function name
2.

Which of the following is the correct syntax to declare a function add that takes two int parameters a and b?

easy
A. int add(int a, int b);
B. int add(a int, b int);
C. int add(int a; int b);
D. int add(int a b);

Solution

  1. Step 1: Recall correct parameter syntax

    In C++, parameters are declared as type name pairs separated by commas inside parentheses.
  2. Step 2: Check each option

    int add(int a, int b); correctly uses int a, int b. Options A, B, and C have syntax errors like missing comma, wrong order, or semicolon.
  3. Final Answer:

    int add(int a, int b); -> Option A
  4. Quick Check:

    Parameters = type name, comma separated [OK]
Hint: Parameters need type and name, separated by commas [OK]
Common Mistakes:
  • Swapping type and name order
  • Using semicolons instead of commas
  • Omitting commas between parameters
3.

What will be the output of the following code?

void greet(std::string name) {
    std::cout << "Hello, " << name << "!" << std::endl;
}

int main() {
    greet("Alice");
    return 0;
}
medium
A. Compilation error
B. Hello, name!
C. Hello, !
D. Hello, Alice!

Solution

  1. Step 1: Understand function call with parameter

    The function greet takes a string parameter name and prints "Hello, " plus the value of name.
  2. Step 2: Check the argument passed

    In main, greet is called with "Alice", so name becomes "Alice".
  3. Final Answer:

    Hello, Alice! -> Option D
  4. Quick Check:

    Parameter value replaces variable in output [OK]
Hint: Function prints parameter value passed in call [OK]
Common Mistakes:
  • Thinking parameter name prints literally
  • Expecting empty output if parameter unused
  • Assuming syntax error due to string parameter
4.

Identify the error in this function declaration:

void printSum(int a, b) {
    std::cout << a + b << std::endl;
}
medium
A. Parameters should be inside square brackets
B. Missing type for parameter b
C. Function must return int
D. Function name is invalid

Solution

  1. Step 1: Check parameter declarations

    Each parameter must have a type and a name. Here, b has no type specified.
  2. Step 2: Identify correct syntax

    The correct declaration is void printSum(int a, int b). Missing type causes a syntax error.
  3. Final Answer:

    Missing type for parameter b -> Option B
  4. Quick Check:

    Every parameter needs a type [OK]
Hint: Each parameter must have a type before its name [OK]
Common Mistakes:
  • Omitting type for second or later parameters
  • Assuming return type must be int
  • Using wrong brackets for parameters
5.

Consider this function that calculates the area of a rectangle:

double area(double width, double height) {
    return width * height;
}

How would you call this function to calculate the area of a rectangle with width 5.0 and height 3.0, and store the result in a variable result?

hard
A. double result = area(width=5.0, height=3.0);
B. double result = area(3.0, 5.0);
C. double result = area(5.0, 3.0);
D. double result = area(5, 3);

Solution

  1. Step 1: Understand parameter order and types

    The function expects width first, then height, both as doubles.
  2. Step 2: Check each call option

    double result = area(5.0, 3.0); passes 5.0 as width and 3.0 as height correctly. double result = area(3.0, 5.0); swaps them. double result = area(width=5.0, height=3.0); uses invalid named arguments (not supported in C++). double result = area(5, 3); passes ints which convert but less precise.
  3. Final Answer:

    double result = area(5.0, 3.0); -> Option C
  4. Quick Check:

    Call with correct order and types [OK]
Hint: Match argument order and types exactly [OK]
Common Mistakes:
  • Swapping argument order
  • Using named arguments (not valid in C++)
  • Passing wrong types without conversion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard