c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I830 materiais10.236 seguidores
Pré-visualização50 páginas
for example you assume that the x-coordinate in the 
above example must always be between 0 and 1023, you could possibly write the set method as follows:
set { if (value >= 0 && value <= 1023) x = value; }
As a final comment on the above class, you should note the constructor. The class has two instance 
variables named, respectively x and y, and the constructor has two parameters which are also called the 
x and y. It gives inside the constructor a name coincidence problem, since there are two things (instance 
variable and parameter) with the same name. The problem is solved with the word this, where this.x 
means the instance variable, while x (with nothing) means the parameter.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
93 
Methods
Parameters
A method\u2019s parameters can be of any type, but there are several kinds of parameter passing. When you define 
a method that you specify the parameters the method should have, and these parameters are called the formal 
parameters and specify the values that the method should operate on. When you call the method, you must 
specify the values (parameters) to be transferred to the method, and they are called for the actual parameters.
Value parameters
In general the parameters are value parameters, for example
static int Max(int a, int b)
{
return a < b ? b : a;
}
When this method is called, there must be transmitted two actual parameters. Exactly what happens is 
that on the stack the system creates a so-called activation block, which contains four basic things:
\u2022	 the return address, so the system knows where the program will continue after the method 
is terminated
\u2022	 a copy of each actual parameter
\u2022	 a place to the return value corresponding to the method\u2019s type
\u2022	 the method\u2019s local variables, if it has local variables
One can outline it as follows:
3
7
7
retur-
adresse
int Max(int a, int b) { ... }
{
 ....
 Max(3, 7);
 ....
}
aktiveringsblok
metode
program
When the program calls the method Max() it creates an activation block on the stack, and the return 
address and the actual parameters are copied to it. Next the method takes the control and starts its work. 
The method then always works on the copies (a and b) located on the stack, and it also means that if 
the method changes these values it alters the copies on the stack, and these changes has no effect in the 
program. When the method performs its return statement, the return value is copied to the activation 
block, then the method terminates and the control is given back to the program and it continues its 
work. Immediately after the program has gained back the control, it can read the return value from the 
activation block, which will then be removed from the stack. Note that this also means that if the method 
creates local variables (which are not the case in this example) they are also gone.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
94 
Methods
Consider as another example, the following method:
public void Swap(int a, int b)
{
int t = a;
a = b;
b = t;
}
When done, it swap the values of the two parameters a and b. If you test the method Swap(), you will 
see that the current parameters are not reversed:
static void Test2()
{
int t1 = 2;
int t2 = 3;
Swap(t1, t2);
Console.WriteLine(&quot;{0} {1}&quot;, t1, t2);
}
If you look at what Swap() creates on the stack, it will swap the a and b
2
3
a
b
t
2
3
2
a
b
t
3
3
2
a
b
t
3
2
2
a
b
t
t=a a=b b=t
© Deloitte & Touche LLP and affiliated entities.
360°
thinking.
Discover the truth at www.deloitte.ca/careers 
© Deloitte & Touche LLP and affiliated entities.
360°
thinking.
Discover the truth at www.deloitte.ca/careers 
© Deloitte & Touche LLP and affiliated entities.
360°
thinking.
Discover the truth at www.deloitte.ca/careers 
© Deloitte & Touche LLP and affiliated entities.
360°
thinking.
Discover the truth at www.deloitte.ca/careers 
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
95 
Methods
but when it happens in the activation block on the stack, the two variables t1 and t2 are unchanged after 
the Swap() is executed.
It should however be aware of what it means to reference types, since the result here may be different 
than expected. Consider the example of the class Point, and the following method, which has a Point 
as a parameter:
static void Mul(Point p, int t)
{
p.X *= t;
p.Y *= t;
}
If you execute the following statements:
Point p = new Point(2, 3);
Mul(p, 2);
Console.WriteLine(p);
you get the result
(4,6)
and thus the method Mul() has changed the Point object. Just it does not seem as the parameter is 
transferred as copy on the stack. The method Mul() has two parameters, and here is the type of the first 
parameter Point and thus a class. The actual parameter is thus a reference, and is placed on the stack, 
which means that the Mul() method refers to the same object as the program. As a result, it is this object 
that is changed.
If you change the Mul() method to the following
public void Mul(Point p, int t)
{
p = new Point(p.X, p.Y);
p.X *= t;
p.Y *= t;
}
the method will create a new Point object and the reference on the stack will refer to this object. When 
Mul() terminates, its activation block is removed, and the reference to the new object is gone, and thus 
the change that Mul() performs. The result is that the object to which the program refers to is unchanged, 
and the program will write
(2,3)
So there is reason to pay attention to what happens if you transfer references as parameters to a general 
value parameter.
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
96 
Methods
Reference parameters
In .NET you can also use true reference parameters. For example you can change the method Swap() 
for the following:
public void Swap(ref int a, ref int b)
{
int t = a;
a = b;
b = t;
}
If you then change the method Test2() so that the actual parameters are references
static void Test3()
{
int t1 = 2;
int t2 = 3;
Swap(ref t1, ref t2);
Console.WriteLine(&quot;{0} {1}&quot;, t1, t2);
}
it are references to the actual parameters that are placed on the stack, and Swap() will swap the values of 
the variables t1 and t2. That means that the changes Swap() performs, is on the objects that the references 
on the stack applies to.
Reference parameters are used instead of value parameters when the changes that a method do on its 
parameters must be maintained in the calling code.
Note that you can combine value parameters and reference parameters so that the method may have 
parameters for both references and values.
out-parameters
In .NET, there is a possibility of using out parameters, which are parameters which may not be initialized 
before a method is called, and instead are assigned a value in the method. The following method creates 
two Point objects assigned to references that are passed as out parameters to the method:
static void Points2(int x1, int y1, int x2, int y2, out Point p1, out Point p2)
{
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
97 
Methods
If the method is performed as follows:
static void Test4()
{
Point p1;
Point p2;
Points2(2, 5, 7, 3, out p1, out p2);
Console.WriteLine(p1);
Console.WriteLine(p2);
}
then p1 and p2 refer to the objects that are created in the method Points2(). Note that p1 and p2 is not 
initialized in the program,