944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2191
  • C++ RSS
Dec 23rd, 2004
0

just a quick question

Expand Post »
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?
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 23rd, 2004
0

Re: just a quick question

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Dec 23rd, 2004
0

Re: just a quick question

Quote 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 ...
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Dec 23rd, 2004
0

Re: just a quick question

WHY NOT --> pass the variable to the function as a parameter!!!! then it will modify the variable as follows

C++ Syntax (Toggle Plain Text)
  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.....)

C++ Syntax (Toggle Plain Text)
  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 ; ;)
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Dec 24th, 2004
0

Re: just a quick question

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
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Random Characters
Next Thread in C++ Forum Timeline: is this a good book





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC