What't the difference

What's the difference behind, output is the same.

Here we have vec1, vec2, vec3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

class Point
{
public:
	double x, y;
};

class Vector
{
public:
	Point Start, End;
};

void offsetVector(Vector &vec3, double offsetX, double offsetY) {
vec3.Start.x += offsetX;
vec3.End.x += offsetX;
vec3.Start.y += offsetY;
vec3.End.y += offsetY;
}


void Print(Vector vec2)
{
cout << "(" << vec2.Start.x << "," << vec2.End.x << ") -> (" << vec2.Start.y << "," << vec2.End.y << ")" << endl;
}


int main()
{
	Vector vec1;
	vec1.Start.x = 2.1;
	vec1.End.x = 2.2;
	vec1.Start.y = 4.1;
	vec1.End.y = 4.2;
	offsetVector(vec1, 1.0, 1.5);
	Print(vec1);
}



And
Here we have only vec1, and the same output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>

using namespace std;

class Point
{
public:
	double x, y;
};

class Vector
{
public:
	Point Start, End;
};

void offsetVector(Vector &vec1, double offsetX, double offsetY) {
vec1.Start.x += offsetX;
vec1.End.x += offsetX;
vec1.Start.y += offsetY;
vec1.End.y += offsetY;
}


void Print(Vector vec1)
{
cout << "(" << vec1.Start.x << "," << vec1.End.x << ") -> (" << vec1.Start.y << "," << vec1.End.y << ")" << endl;
}


int main()
{
	Vector vec1;
	vec1.Start.x = 2.1;
	vec1.End.x = 2.2;
	vec1.Start.y = 4.1;
	vec1.End.y = 4.2;
	offsetVector(vec1, 1.0, 1.5);
	Print(vec1);
}
Last edited on
The difference is purely cosmetic. Parameter names do not connect them with similarly named variables in other functions.
There is no difference as far as I can see. The code is identical except you changed the name of your variable.

What might be throwing you off is a scoping issue.

A variable is only "seen" by the program within its current scope. Typically the scope is determined by the surrounding {curly braces}

For example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

int main()
{
  Vector vec;  // since vec is defined here, its scope is limited to inside the main function

  //...
} // <- scope ends, 'vec' no longer exists

void Print( Vector v )
{
  // 'v' exists in this scope.  But main's 'vec' no longer exists
}

void SomeOtherFunction( Vector vec )
{
  // this function has its own 'vec', but it is completely different from the 'vec' declared
  //   in main.  Remember main's 'vec' is out of scope and so it no longer exists.
}



When you pass a variable as a parameter to a function, it basically does an assignment. This allows you to give variables to areas of code that are normally outside their scope.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Print( Vector v )
{
  // Print has its own Vector 'v'
}

int main()
{
  Vector vec;  // main's vec

  //...

  Print( vec );  // we pass 'vec' to Print.
   //  this basically does an assignment.. something like:
   //  Print_v = main_vec;
   //  This is done before the code inside the Print function is run.  So when you use
   //  'v' inside the Print function, it contains the same data as main's 'vec'

  // And say for example you have a 2nd vector here:
  Vector vec2;  // a second vector

  //...

  Print( vec2 );  // this time we're giving Print 'vec2' as a parameter.  So now:
    // Print_v = main_vec2;
    //  Now 'v' in Print will equal 'vec2' in main.
}
Thank you very much for your fast responses.
Here are good explanation about scoping https://www.youtube.com/watch?feature=player_embedded&v=CsQrTLde-dM but in python language. But the idea are the same.
Topic archived. No new replies allowed.