Bird
Raised Fist0
C++programming~10 mins

Function calling in C++ - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the function greet.

C++
#include <iostream>

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

int main() {
    [1];
    return 0;
}
Drag options to blanks, or click blank then click option'
Agreet()
Bgreet
Cgreet();()
Dcall greet()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the parentheses when calling the function.
Trying to call the function with extra words like 'call'.
2fill in blank
medium

Complete the code to call the function add with arguments 5 and 3.

C++
#include <iostream>

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

int main() {
    int result = [1];
    std::cout << result << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Aadd(5, 3)
Badd(5 3)
Cadd(5; 3)
Dadd 5, 3
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses or commas between arguments.
Using semicolons or spaces instead of commas.
3fill in blank
hard

Fix the error in calling the function multiply with arguments 4 and 7.

C++
#include <iostream>

int multiply(int x, int y) {
    return x * y;
}

int main() {
    int product = [1];
    std::cout << product << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Amultiply(4 7)
Bmultiply(4, 7)
Cmultiply(4;7)
Dmultiply 4, 7
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or semicolons instead of commas between arguments.
Omitting parentheses around arguments.
4fill in blank
hard

Fill both blanks to call divide with arguments 10 and 2, and store the result.

C++
#include <iostream>

double divide(double a, double b) {
    return a / b;
}

int main() {
    double result = [1];
    std::cout << [2] << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
Adivide(10, 2)
Bresult
Cdivide(10 2)
D10 / 2
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to print the function call directly instead of the stored result.
Using spaces instead of commas between arguments.
5fill in blank
hard

Fill all three blanks to call concat with two strings and print the result.

C++
#include <iostream>
#include <string>

std::string concat(std::string s1, std::string s2) {
    return s1 + s2;
}

int main() {
    std::string combined = [1];
    std::cout << [2] << std::endl;
    return [3];
}
Drag options to blanks, or click blank then click option'
Aconcat("Hello, ", "World!")
Bcombined
C0
Dconcat("Hello" "World")
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting commas between string arguments.
Trying to print the function call directly instead of the variable.
Returning a non-zero value from main.

Practice

(1/5)
1. What does calling a function in C++ do?
easy
A. Runs the code inside the function
B. Deletes the function
C. Creates a new function
D. Changes the function name

Solution

  1. Step 1: Understand what a function call means

    Calling a function means telling the program to run the code inside that function.
  2. Step 2: Identify the effect of calling a function

    When called, the function's instructions execute, performing its task.
  3. Final Answer:

    Runs the code inside the function -> Option A
  4. Quick Check:

    Function call = run function code [OK]
Hint: Function call runs its code block immediately [OK]
Common Mistakes:
  • Thinking calling creates or deletes functions
  • Confusing function call with function definition
  • Assuming function call changes function name
2. Which of the following is the correct way to call a function named printMessage in C++?
easy
A. printMessage();
B. printMessage;
C. call printMessage();
D. function printMessage();

Solution

  1. Step 1: Recall the syntax for calling a function

    In C++, to call a function, write its name followed by parentheses and a semicolon.
  2. Step 2: Check each option

    printMessage(); uses the correct syntax: printMessage();. Others are incorrect or invalid syntax.
  3. Final Answer:

    printMessage(); -> Option A
  4. Quick Check:

    Function call syntax = name + () + ; [OK]
Hint: Function calls always end with parentheses and semicolon [OK]
Common Mistakes:
  • Omitting parentheses when calling functions
  • Adding keywords like 'call' or 'function' incorrectly
  • Missing semicolon at the end
3. What is the output of this C++ code?
void greet() {
    std::cout << "Hello!" << std::endl;
}

int main() {
    greet();
    return 0;
}
medium
A. Compilation error
B. greet()
C. No output
D. Hello!

Solution

  1. Step 1: Understand what the function greet() does

    The function prints "Hello!" followed by a new line.
  2. Step 2: See what main() does

    Main calls greet(), so "Hello!" is printed to the screen.
  3. Final Answer:

    Hello! -> Option D
  4. Quick Check:

    Function call prints "Hello!" [OK]
Hint: Function call runs its print statements [OK]
Common Mistakes:
  • Expecting function name printed instead of output
  • Thinking no output because no return value
  • Assuming compilation error without syntax issues
4. Identify the error in this code snippet:
void sayHi() {
    std::cout << "Hi!" << std::endl;
}

int main() {
    sayHi;
    return 0;
}
medium
A. Function sayHi is not defined
B. Missing parentheses when calling sayHi
C. Missing semicolon after sayHi
D. Incorrect return type in main

Solution

  1. Step 1: Check how sayHi is called

    In main(), sayHi is written without parentheses, so it is not called.
  2. Step 2: Understand correct function call syntax

    To call sayHi, it must be sayHi(); with parentheses.
  3. Final Answer:

    Missing parentheses when calling sayHi -> Option B
  4. Quick Check:

    Function call needs parentheses [OK]
Hint: Always use () to call a function [OK]
Common Mistakes:
  • Forgetting parentheses on function calls
  • Thinking function name alone calls it
  • Ignoring compiler errors about function usage
5. Given this function:
int add(int a, int b) {
    return a + b;
}

What is the correct way to call add to get the sum of 5 and 3 and store it in result?
hard
A. int result = add(5 + 3);
B. int result = add(5);
C. int result = add(5, 3);
D. int result = add();

Solution

  1. Step 1: Check the function parameters

    The function add requires two integers as input: a and b.
  2. Step 2: Match the call with two arguments

    int result = add(5, 3); passes both 5 and 3 correctly. Others pass wrong number or format of arguments.
  3. Final Answer:

    int result = add(5, 3); -> Option C
  4. Quick Check:

    Function call needs correct number of arguments [OK]
Hint: Match function parameters count exactly when calling [OK]
Common Mistakes:
  • Passing fewer or more arguments than function needs
  • Passing expressions without commas for multiple args
  • Calling function without any arguments when required
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