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)
- in C++ Write the definition of a value-returning function that accepts two integer parameters and returns the larger one divided by the smaller one.arrow_forwardA program uses a function named convert() in addition to its main function. The function main() declares the variable x within its body and the function convert declares two variables y and z within its body, z is made static. A fourth variable m is declared ahead(ie at top) of both the functions. State the visibility and lifetime of each of these variables. Write the code in C++ language. Thanks in advancearrow_forwardWhat does it mean to provide parameters to a function in the right order when it accepts more than one?arrow_forward
- Invoking a function that takes many arguments, the order in which parameters are supplied is crucial.arrow_forwardWrite a C++ program to facilitate the usage of several string-handling functions. The program should present a menu for the choices available. The options might be to create, insert, append, erase, reverse, and print. The program should ask the user what required by the function to prepare the real function call. For instance, for the insert function, the string might be printed on the screen as a reminder first, and then the start and finish indexes might be requested from the user. You should be aware of the run-time issues that might result due to user mistakes. Try to prevent them!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
- pointers as Arguments:In the C programming language there is no pass-by-reference syntax to passa variable by reference to a function. Instead a variable is passed by pointer(just to be confusing, sometimes passing by pointer is referred to as pass byreference). This Practice Program asks you to do the same thing as C.Here is the header for a function that takes as input a pointer to an integer:1. void addOne (int ∗ptrNum )Complete the function so it adds one to the integer referenced by ptrNum.Write a main function where an integer variable is defined, give it an initialvalue, call addOne, and output the variable. It should be incremented by 1.arrow_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_forwardUsing a pointer as the return value is considered a poor practise in C. For example, how does dynamic memory allow us to return an object pointer from a function safely?arrow_forward
- C 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_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