User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,589 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,660 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1630 | Replies: 4
Reply
Join Date: Dec 2004
Posts: 458
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

just a quick question

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2004
Posts: 1,514
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Rep Power: 10
Solved Threads: 48
Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: just a quick question

  #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  
Join Date: Dec 2004
Posts: 458
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: just a quick question

  #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  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: just a quick question

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

#include <iostream>
using namespace std;

int function(int var)
{
    return (var * var * var);
}

int main(void)
{
    int x = 2; // our variable
    cout << "X before call " << x << "\n";
    x = function(x); // pass our x to the function, put result in x, alternative is to use pass by reference
    cout << "X after call " << x << "\n";
    return 0;
}

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.....)

void function(int var)
{
    int result = var ^ 3;
    var = result;
} // change the function (it doesnt return anything, this probably is what you are after)
// calling it
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 7:59 pm. Reason: Missing ; ;)
http://sales.carina-e.com

no www
no nonsense

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

Re: just a quick question

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:09 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC