| | |
just a quick question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
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
[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
C++ Syntax (Toggle Plain Text)
#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.....)
C++ Syntax (Toggle Plain Text)
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 8: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:
![]() |
Similar Threads
- quick question (Python)
- Quick question (C++)
- quick question w\ a program (Java)
- nevermind: ignore arrays problem - quick question (C++)
- A quick question about "rank" (DaniWeb Community Feedback)
- Quick question on a loop. (C++)
- Quick Question: Is J# the same thing as Java? (Java)
- A quick question (Game Development)
- quick question (C++)
- 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
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







thanks for your help, most appricated