•
•
•
•
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
![]() |
•
•
Join Date: Dec 2004
Posts: 458
Reputation:
Rep Power: 4
Solved Threads: 5
•
•
•
•
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 ...
WHY NOT --> pass the variable to the function as a parameter!!!! then it will modify the variable as follows
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.....)
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
#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 xthe 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:
no www
no nonsense
coming soon to a pc near you! :cool:
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- A quick question about "rank" (DaniWeb Community Feedback)
- Quick Question: Is J# the same thing as Java? (Java)
- Quick HijackThis question (Viruses, Spyware and other Nasties)
- A quick question (Game Development)
- quick question (C++)
- quick question (Geeks' Lounge)
- Question about 321Studios (Windows Software)
- AGP support video card question (Monitors, Displays and Video Cards)
- Quick question (Troubleshooting Dead Machines)
- Laptop LCD built into a car? (Monitors, Displays and Video Cards)
Other Threads in the C++ Forum
- Previous Thread: Random Characters
- Next Thread: is this a good book



thanks for your help, most appricated
Linear Mode