Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Global variables

Posted on 2011-03-21
4
Medium Priority
?
380 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 600 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 1400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

704 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