just a quick question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

just a quick question

 
0
  #1
Dec 23rd, 2004
Hello,

i'm wondering if you can delcre a varible in int main and use it in a function?

eg
[PHP]

int main()
{
x = 7

cout << x<<endl;

return 0;
}

function --

{ ....blah blah

}

[/PHP]
does something to x delcared in local scope (int main)

Or would I have to declare it globally?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: just a quick question

 
0
  #2
Dec 23rd, 2004
Hello,

Sure can. Actually a good programming procedure. main() is a function, just like factorial(), or maybe openfile() or fillarray(). The thing that makes main special is that it is "called" first.

What you want to do is just fine. Local variables are the way to go!

Christian
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: just a quick question

 
0
  #3
Dec 23rd, 2004
Originally Posted by kc0arf
Hello,

Sure can. Actually a good programming procedure. main() is a function, just like factorial(), or maybe openfile() or fillarray(). The thing that makes main special is that it is "called" first.

What you want to do is just fine. Local variables are the way to go!

Christian
just thought i'd try and make something to be more clearer:

[PHP]#include <iostream>

int function int

using namespace std;

int main

{

int x = 5;
cout << "X before function <<x<<endl;


function()

return 0;

}

int function (int y)
{
int y;

y = x*x*x

cout << this is now the value of y << y;

}[/PHP]

however this should moan about int x not been defined ...
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: just a quick question

 
0
  #4
Dec 23rd, 2004
WHY NOT --> pass the variable to the function as a parameter!!!! then it will modify the variable as follows

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int function(int var)
  5. {
  6. return (var * var * var);
  7. }
  8.  
  9. int main(void)
  10. {
  11. int x = 2; // our variable
  12. cout << "X before call " << x << "\n";
  13. x = function(x); // pass our x to the function, put result in x, alternative is to use pass by reference
  14. cout << "X after call " << x << "\n";
  15. return 0;
  16. }

this program cubes the variable x and displays before/after results.
you can also use a call by reference method so that a function modifys the variables you pass into it (wheras I have just passed the variable and got a result, which i assign to the variable.....)

  1. void function(int var)
  2. {
  3. int result = var ^ 3;
  4. var = result;
  5. } // change the function (it doesnt return anything, this probably is what you are after)
  6. // calling it
  7. function(&x); // sends x to the function. & means modify the x and keep the result in x

the problem you had before is that the X you are using in the function is NOT the global x as it is in a different statement block. using parameters in the functions is the best way to go, and the reference method is the easier method to use. Hope this helps
Last edited by 1o0oBhP; Dec 23rd, 2004 at 8:59 pm. Reason: Missing ; ;)
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: just a quick question

 
0
  #5
Dec 24th, 2004
referencing is the same thing has pointers? If so i hate that way, but thers nothing better than practicing thanks for your help, most appricated
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC