You are not logged in.
I am a beginner ok....
#include <iostream>
using namespace std;
void print_square(int v)
{
cout<< v << '\t' << v*v<<'\n';
}
int main()
{
for (int i=0; i<100; ++i) print_square(i);
}
I see this is a function obviously but what is int v for? Int v never really gets used? In main() its uses print_square(i) before print_square uses int v right? So whats int v there for?
Last edited by generic_ (2009-03-27 03:09:01)
I'm just lost n00b!
Offline
In your function definition:
void print_square(int v)
You say this function needs an "int" passed to it and for the purpose of the function definition we will refer to the value passed in as v. So the actual name of the variable passed to the function can be anything, but inside the function definition it will be call v.
Clear as mud?
Offline
Yup: It's like saying:
f(x) = x + 3
In math and then somewhere on the page using:
y = f(3)
therefore y = 6
Matt
"It is very difficult to educate the educated."
Offline
Oh I understand now. So int v is the name of the variable in the function and V will equal whatever is passed through. This won't stop me from using a another variable nameed int v somewhere else in the program will it?
I'm just lost n00b!
Offline
No, you can use v elsewhere in your program.
Offline
So int v is the name of the variable in the function and V will ....
Close, but C++ is case-sensitive. A person could get very confused referring to v as V. On the original question, you may want to review some material on variable scope. The variables v and i both have local scope, which means they only exist within the print_square and main functions, respectively. Other parts of program are unaffected by anything having to do with them. It is also possible to create global variables which can be accessed from any part of your program, just as functions can.
Offline
Thank you guys my book didn't really clarify on this. You guys explained it perfectly, the big picture is starting to come together.
I'm just lost n00b!
Offline
Clear as mud?
:P
sounds about right for anything in C++ hehe
Excellent explanation by using F(x) though...thats much easier than every other way I've seen it presented...
Offline