Solved

Global variables

Posted on 2011-03-21
4
369 Views
Last Modified: 2012-05-11
Hi guys: Can any one please tell me how to i define the variable number as a global variable ? Also how to pass the number variable value to the getDoubleNumber function ?

Thanks.

#include <iostream>
using namespace test;

int getDoubleNumber();

int main()
{
      int number       = 0;
      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber();
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber()
{
      return number * 2;
}

0
Comment
Question by:mustish1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:niralbhavsar
niralbhavsar earned 150 total points
ID: 35186052
This is what you requested:
#include <iostream>
using namespace test;

int getDoubleNumber();
int number       = 0;

int main()
{

      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(number)
{
      return number * 2;
}

Open in new window

Its preferred if you don't use a global variables for elegant programming. You don't need a global variable if your code looks like this:
#include <iostream>
using namespace test;

int getDoubleNumber();


int main()
{
	  int number       = 0;
      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(number)
{
      return number * 2;
}

Open in new window


0
 

Author Comment

by:mustish1
ID: 35186093
still get a syntax error on line
int getDoubleNumber(number)
-------------------------------------------
#include <iostream>
using namespace test;

int getDoubleNumber();
int number       = 0;

int main()
{

      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(number)
{
      return number * 2;
}
0
 
LVL 8

Accepted Solution

by:
crysallus earned 350 total points
ID: 35186225
That code from niralbhavsar is a little incorrect. You need to include the data type of the argument in the function declaration and definition:

#include <iostream>
using namespace test;

int getDoubleNumber(int);

int main()
{
          int number       = 0;
      int doubleNumber = 0;
      cout << "Enter a number: ";
      cin >> number;

      doubleNumber = getDoubleNumber(number);
      cout << "Answer: " << doubleNumber << endl;

      system("pause");
      return 0;
}
int getDoubleNumber(int number)
{
      return number * 2;
}

Open in new window

0
 

Author Closing Comment

by:mustish1
ID: 35188705
Thanks
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question