954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

just a quick question

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<

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 
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

int function int

using namespace std;

int main

{

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

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

referencing is the same thing has pointers? If so i hate that way, but thers nothing better than practicing :D thanks for your help, most appricated

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You