0
0
C++programming~15 mins

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

Choose your learning style9 modes available
Function parameters
📖 Scenario: You are creating a simple calculator program that adds two numbers. You will learn how to use function parameters to pass values into a function.
🎯 Goal: Build a program that defines a function add which takes two numbers as parameters and returns their sum. Then call this function with specific numbers and print the result.
📋 What You'll Learn
Create a function named add that takes two int parameters named a and b
The function add should return the sum of a and b
Call the function add with the numbers 7 and 5
Print the result of the function call
💡 Why This Matters
🌍 Real World
Functions with parameters are used everywhere in programming to reuse code and perform tasks with different inputs, like calculators, games, and apps.
💼 Career
Understanding function parameters is essential for writing clean, reusable code, a key skill for any software developer.
Progress0 / 4 steps
1
Create the add function with parameters
Write a function named add that takes two int parameters called a and b. The function should return an int. Do not write the function body yet.
C++
Need a hint?

Start by writing the function header int add(int a, int b); and a main function.

2
Implement the add function to return the sum
Complete the add function so it returns the sum of the parameters a and b.
C++
Need a hint?

Use the return statement to send back the sum of a and b.

3
Call the add function with 7 and 5
Inside the main function, call the add function with the arguments 7 and 5. Store the result in an int variable named result.
C++
Need a hint?

Use int result = add(7, 5); inside main to call the function and save the sum.

4
Print the result
Add a std::cout statement inside main to print the value of result. Include #include <iostream> at the top of the program.
C++
Need a hint?

Use std::cout << result << std::endl; to print the sum.