Solved

Global variables

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now