127k views
3 votes
State the output of the following program, and write the type of parameter or variable used.

#include
void DoA( );
void DoB ( );
void DoC( int&); _________________
void DoD( int); _________________
int x; _________________
int main( )
{
x=15; __________________
DoC(x);
cout<<"x= "<x=16; __________________
DoD(x);
cout<<"x= "<x=17;
DoB( );
cout<<"x= "<x=18;
DoA( );
cout<<"x= "<Output:
return 0;
}
void DoA( )
{ x=7; } __________________
void DoB ( )
{ int x; __________________
x=5; }
void DoC ( int& a)
{ a=3; }
void DoD ( int b)
{ b=4; }

User JohnColvin
by
7.8k points

1 Answer

5 votes

Final answer:

The program utilizes a mix of global variables, pass-by-reference, and pass-by-value parameters. The outputs, after respective function calls in the main function, would be 'x= 3', 'x= 16', 'x= 17', and 'x= 7'. The functions modify the variable x in different ways, demonstrating the concepts of global variables and parameter passing in C++.

Step-by-step explanation:

The student has provided a C++ program and is asking to state the output of the program and to describe the type of parameters or variables used. The program consists of a global variable x, a main function, and four other functions: DoA, DoB, DoC, and DoD.

The main function initializes the variable x to 15 and then calls the function DoC, which is a function that takes an integer reference as a parameter. This changes the value of x to 3. The output after this function call will be 'x= 3'.

Next, the value of x is set to 16 and the DoD function is called with x as an argument. However, DoD takes an integer by value, so it does not change the global x. The output remains 'x= 16'.

The value of x is then set to 17, and DoB is called. This function declares a local variable also named x and sets its value to 5, but it does not change the global variable. Therefore, the output remains 'x= 17'.

Finally, the value of x is set to 18, and the DoA function is called, which changes the global variable x to 7. The final output will be 'x= 7'.

The program concludes with return 0, indicating successful execution.

Overall, the different functions use different types of parameters: DoC uses a reference parameter, and DoD uses a pass-by-value parameter. The variables used in the functions are either global or local, depending on whether they are defined inside or outside of a function body.

User James Gu
by
8.3k points