Question

Give the output for the following code assume int globalvar 10: above the function prototypes void foo(int&); void bar(int);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question:

Give the output of the following code:
#include <iostream>
using namespace std;

//Declared an globalVar intialised with
//value 10 above the function prototypes
int globalVar=10;
void foo(int&);
void bar(int);
int main()
{
int globalVar = 5;
foo(globalVar);
cout<<globalVar<<endl;
bar(globalVar);
cout<<globalVar<<endl;
return 0;
}
void foo(int& x)
{
x=globalVar+x;
cout<<globalVar<<endl;
return;
}
void bar(int x)
{
x=globalVar+x;
cout<<globalVar<<endl;
return;
}

The output of the following code is:
10
15
10
15

  • All the execution starts from the main
  • In the main, we have defined a variable named globalVar as 5, then we are calling the function foo() with the parameter globalVar()
  • In the function foo(), we are incrementing the globalVar with the value of x(which is globalVar) as 10+5 ==> 15, and then we are printing the value of globalVar which is 10.
  • In the main, we are printing the value of globalVar as 15
  • Again, we are calling the function bar() with the parameter globalVar
  • In the function bar(), we are again updating the value of x as globalVar with x ==> 10+5 as 15, and then we are printing the value of globalVar which is 10
  • In the main, we are printing the value of globalVar as 15

Please check the compiled program and its output for your reference:
1 main.cpp #include <iostream> 2 using namespace std; 3 4 //Declared an globalVar intialised with 5 l/value 10 above the func
Output:
input 10 15 10 15 ... Program finished with exit code o Press ENTER to exit console. I
(Feel free to drop me a comment, If you need any help)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Add a comment
Know the answer?
Add Answer to:
Give the output for the following code assume int globalvar 10: above the function prototypes void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void)...

    Consider the following program: # include <iostream> int x = 3, y = 5; void foo(void) { x = x + 2; y = y + 4; } void bar(void) { int x = 10; y = y + 3; foo( ); cout << x << endl; cout << y << endl; } void baz(void) { int y = 7; bar( ); cout << y << endl; } void main( ) { baz( ); } What output does this program...

  • Design a swap function with the following interface: void swap( int *x,    int *y) { }...

    Design a swap function with the following interface: void swap( int *x,    int *y) { } In your main( ), you perform the following test: int main( ) {             int a = 10, b = 20;             cout << “a = “ << a << “   b= “   << b   << endl;             swap( a, b);             cout << “a = “ << a << “   b= “   << b   << endl;             return 0;                         … }

  • What is the output of the following program? int main(void) { int x, y, z; x...

    What is the output of the following program? int main(void) { int x, y, z; x = 5; y= test(x); z= x + y; cout<< setw(4)<<x <<” + “<<setw(4)<<y<<”=” << setw(4)<<z<<endl; return 0; } int test (int x) { int w; w = x + 10; return (w); }

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • Three of these function overloads are considered identical by the compiler and would conflict with each...

    Three of these function overloads are considered identical by the compiler and would conflict with each other. The other would be considered different- choose the one that is considered different from others. Int foo ( int x, double y ) ; Int foo ( double x, int y ) ; Void foo ( int x, double y ) ; Int foo ( int day, double temp ) ; Consider the following incomplete code: Int f ( int number ) }...

  • C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int*...

    C++ 1. A?B?C?D? which one is correct 2. 3A, 3B #include<iostream> using namespace std; void swap0(int* ptri, int* ptr2) { int *temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap1(int ptri, int ptr2){ int temp; temp = ptri; ptr1 = ptr2; ptr2 = temp; portion void swap2(int *&ptri, int *&ptr2){ int* temp; temp = ptr1; ptr1 = ptr2; ptr2 = temp; void swap3(int &ptri, int &ptr2) { int temp; temp = ptr1; ptr1 = ptr2; ptr2 =...

  • C++ question #include <iostream> using namespace std; void printReverse(int); int main() {     int number = 12345;...

    C++ question #include <iostream> using namespace std; void printReverse(int); int main() {     int number = 12345;     cout << "Original : " << number << endl;     cout << "Reversed : ";     printReverse(number); } void printReverse(int x) {     if (x == 0)        return;     cout << x % 10;     printReverse(x /= 10);     } Modify the above recursive program to output the number in the same order. Note that the program still should break up the number and then output it in the...

  • Use the code below to answer the questions that follow. Assume that all proper libraries and...

    Use the code below to answer the questions that follow. Assume that all proper libraries and name spaces are included and that the code will compile without error. Within the main function, for the variable int last_sid , write in the last digit of your SMC student ID. int foo(int a, int b) { //First int c = a+b; while(c>=3) c-=3; return c; } //------------------------------------ char foo(string a, int b) { //Second return a[b]; } //------------------------------------ string foo(int b, string...

  • 12. What is the output of the following C++ code? int x; int y; int *p...

    12. What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; *p = 35; *q = 98; *p = *q; cout << x << " " << y << endl; cout << *p << " " << *q << endl; 13. What is the output of the following C++ code? int x; int y; int *p = &x; int *q = &y; x = 35; y = 46; p...

  • points): Show the output of the code below as it would appear on the monitor. int...

    points): Show the output of the code below as it would appear on the monitor. int main cout <<" <<endl: int wildcat 2: while (wildcat > 5) cout << wildcat <<endl; wildcat++ cout <K*<< endl; int jayhavk 5i do cout << jayhawk <s endl: while (jayhawk0) cout <<*" << endl; int wolverine l: while (wolverine 0 &&wolverine < 10) cout << wolverine <<endl: wolverine2: cout <<" <<endl: for (int zag-4; zag>0; zag_) cout <<****" << endl; for (int i-10; i<-30;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT