0

Hello I am a beginner at C++ and I was given these questions to do for my homework. I would appreciate it if you could help me answer these questions.

Consider the following code when answering questions 1-5

#include <iostream>
using namespace std;

int someVar = 10

void f(intx, int&y);
int g(int x);

int main()
{
   int someVar = 3
   int a = 31, b = 99;

   f(a,b); //See Question 1

   cout << "a = " << a << ", b = " << b << endl;

   f(2*a-3,b); // See question 2

   cout << " a = " << a << ", b = " << b << endl;
   cout << "the value of someVar is" << someVar << endl; // See Question 3
   cout << "g(3) returns the value" << g(3) << endl; // See Question 4
   return 0;
}

void f(int x, int&y)
{
   int someVar = 5;
   x = 2;
   y = 5*someVar;
}

int g(int x)
{
   return someVar*x;
}

Question 1
What will be the value stored in variables "a" and "b" after the call f(a,b)?

Question 2
What will be the value stored in variables "a" and "b" after the call f(2*a-3,b)?

Question 3
What will be the value printed out for someVar?

Question 4
What will be returned by the call to function g(3)?

Question 5
What is the difference between local and global variables? Use examples from the program to explain.

Edited by __avd: Corrected the [code] tags.

4
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by PrimePackster
0

Asking us to solve your homework assignment is cheating! You need to make the first step / effort to solve / answer the question, and then we might help you.

0

Sincerely, the easiest way to get four of the five answers is to compile the code and step through it in the debugger.
It will tell you a lot.

0

Ok! Just as the above comment states, find the first 4 questions yourself.

5) Every variable will be having their scope. Scope refers to the life of the variable in a code. Where the variable can be used. A variable declaired outside the any funtions in a code, is having a global scope, meaing it can be used inside any funtions, structures, even inside blocks.

Local scope: As name suggest, these variables doesn't have global usability. They are restricted to the block of code inside which it was declaired, like UD functions, main(), block or compound statements etc....

0

Now that i have told you what global variables and local variables are, you do the rest of the work by recognising the examples in code.... :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.