📖 Scenario: Imagine you are creating a simple calculator program that can add two numbers. You will write a function to add numbers and then call it to get the result.
🎯 Goal: Build a program that defines a function to add two integers and calls this function to display the sum.
📋 What You'll Learn
Define a function named add that takes two int parameters and returns their sum.
Create two integer variables with exact values 5 and 7.
Call the add function with these two variables.
Print the result of the function call.
💡 Why This Matters
🌍 Real World
Functions help organize code into reusable blocks, like a calculator that can add many pairs of numbers without rewriting code.
💼 Career
Understanding function calling is essential for writing clean, modular code in any software development job.
Progress0 / 4 steps
1
Create two integer variables
Create two integer variables called num1 and num2 with values 5 and 7 respectively.
C++
Hint
Use int to declare variables and assign the exact values.
2
Define the add function
Define a function named add that takes two int parameters named a and b and returns their sum as an int.
C++
Hint
Write the function header with two parameters and return their sum inside the function body.
3
Call the add function and store result
Call the add function with num1 and num2 as arguments and store the result in an integer variable called sum.
C++
Hint
Use the function name add with the two variables inside parentheses and assign the result to sum.
4
Print the sum
Print the value of the variable sum using std::cout.
C++
Hint
Use std::cout << sum << std::endl; inside main() to print the sum.
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
Step 1: Understand what a function call means
Calling a function means telling the program to run the code inside that function.
Step 2: Identify the effect of calling a function
When called, the function's instructions execute, performing its task.
Final Answer:
Runs the code inside the function -> Option A
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
Step 1: Recall the syntax for calling a function
In C++, to call a function, write its name followed by parentheses and a semicolon.
Step 2: Check each option
printMessage(); uses the correct syntax: printMessage();. Others are incorrect or invalid syntax.
Final Answer:
printMessage(); -> Option A
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