Solved

Global variables

Posted on 2011-03-21
4
353 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
  • 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

770 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