You are not logged in.

#1 2009-03-27 01:41:16

generic_
Member
From: Jacksonville,FL US
Registered: 2008-12-21
Posts: 182

[SOLVED][C++] What the purpose of int v here?

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

#2 2009-03-27 01:46:05

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,384
Website

Re: [SOLVED][C++] What the purpose of int v here?

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

#3 2009-03-27 02:04:18

mrunion
Member
From: Jonesborough, TN
Registered: 2007-01-26
Posts: 1,938
Website

Re: [SOLVED][C++] What the purpose of int v here?

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

#4 2009-03-27 02:15:25

generic_
Member
From: Jacksonville,FL US
Registered: 2008-12-21
Posts: 182

Re: [SOLVED][C++] What the purpose of int v here?

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

#5 2009-03-27 02:24:24

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,384
Website

Re: [SOLVED][C++] What the purpose of int v here?

No, you can use v elsewhere in your program.

Offline

#6 2009-03-27 03:04:04

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: [SOLVED][C++] What the purpose of int v here?

generic_ wrote:

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

#7 2009-03-27 03:08:28

generic_
Member
From: Jacksonville,FL US
Registered: 2008-12-21
Posts: 182

Re: [SOLVED][C++] What the purpose of int v here?

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

#8 2009-03-27 12:10:09

thisperishedmin
Member
Registered: 2008-11-04
Posts: 164

Re: [SOLVED][C++] What the purpose of int v here?

Allan wrote:

Clear as mud?

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

Board footer

Powered by FluxBB