Program Plan:
- Include required header files.
- Declaration of function prototype.
- Definition for function “main()”.
- Declare and initialize the variable “var”.
- Print the value before calling function
- Call the function “addOne()”.
- Print the value after calling function.
- Return the value “0”.
- Definition of function “addOne()”.
- Increment the pointer variable.
The given program is to add one to the integer referenced by “ptrNum” by using reference parameter syntax.
Explanation of Solution
//Include required header files
#include<iostream>
using namespace std;
//Declaration of function header
void addOne(int *ptrNum);
//Definition of function main()
int main()
{
//Declare and initialize the variable "var".
int var = 10;
//Print the value before calling function
cout << "Value before calling function = " << var << '\n';
//Call the function
addOne(&var);
//Print the value after calling function
cout << "Value after calling function = " << var << '\n';
//Return the value "0"
return 0;
}
//Definition of function "addOne()"
void addOne(int *ptrNum)
{
//Increment the pointer variable
*ptrNum = *ptrNum + 1;
}
Output:
Value before calling function = 5
Value after calling function = 6
Want to see more full solutions like this?
Chapter 9 Solutions
Problem Solving with C++ (9th Edition)
- What does it mean to provide parameters to a function in the right order when it accepts more than one?arrow_forwardInvoking a function that takes many arguments, the order in which parameters are supplied is crucial.arrow_forwardIn C++, If you had the double-pointer above and also had these variables: type x, * q; And had executed these statements: q = &x; p = &q; How would a function given p by value be able to change the contents of x?arrow_forward
- In C++ programing language, Write the definition of the function template that swaps the content of two variablesarrow_forwardIn C/C++, Function overloading is the process of creating several functions that have exactly the same name. In order to overload successfully, each function must have: A. A different parameter list B. A different number of parameters and a different return type C. An integer on the end of the function name, such as func1()and func2() D. Either A or B E. Both A and Carrow_forwardPlease solve it in C languagearrow_forward
- A specific class of C++ variable is a pointer variable. Do you understand how to utilize it?arrow_forwardSolve in c programming language.arrow_forwardC Program Functions using Pointers Create a function modify that accepts an integer and divides the integer by 2 if it is even. If the integer is odd, add one and divide it by 2. The function does not return anything. In the main function, write a program that asks for an integer input and call the modify function by passing in the reference of that variable as a parameter. An initial code is provided for you. Just fill in the blanks. Input 1. One line containing an integer input Output Enter a number: 5 Before: 5 After: 3arrow_forward
- Please explain the concept of pointers in a function in C++.arrow_forwardIn C++, Write a complete program to call a function that swaps the values stored in two variables. The function accepts two parameters x and y which are to be swapped. The parameters can be of any data-type.arrow_forwardwrite a program in c++ to pass two numbers as parameters to a function which is used to swap the twonumbers.arrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr