0
0
C++programming~15 mins

Function calling in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Function calling
📖 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++
Need a 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++
Need a 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++
Need a 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++
Need a hint?

Use std::cout << sum << std::endl; inside main() to print the sum.