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.

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.

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.

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....

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.