Notice that the weekly hours and salary values are
the same before and after calling the Earnings() method.
When you declare a variable in a program, the
compiler reserves an amount of space for that variable. If you need to
use that variable somewhere in your program, you call it and make use of
its value. There are two major issues related to a variable: its value
and its location in the memory. The location of a variable in memory is referred to
as its address.
If you supply the argument using its name, the
compiler only makes a copy of the argument’s value and gives it to the
calling method. Although the calling method receives the argument’s
value and can use it in any way, it cannot (permanently) alter it. C#
allows a calling method to modify the value of a passed argument if you
find it necessary. If you want the calling method to modify the value of
a supplied argument and return the modified value, you should pass the
argument using its reference.
To pass an argument as a reference, when defining
and when calling the method, precede the argument's data type with the ref
keyword. You can pass 0, one, or more arguments as reference in the
program or pass all arguments as reference. The decision as to which
argument(s) should be passed by value or by reference is based on
whether or not you want the called method to modify the argument and
permanently change its value.
Another option consists of passing an
argument using the out keyword. Here is an example:
using System;
class Exercise
{
static void Initializer(out double n)
{
n = 128.44;
}
public static int Main()
{
double Number = 15.25;
Console.WriteLine("Number = {0}", Number);
return 0;
}
}
If you pass an argument with out, any modification
made on the argument would be kept when the method ends. When calling a method
that takes an out argument, precede the argument with the out keyword. Here is
an example:
using System;
class Exercise
{
static void Initializer(out double n)
{
n = 128.44;
}
public static int Main()
{
double Number = 15.25;
Console.WriteLine("Number = {0}", Number);
Initializer(out Number);
Console.WriteLine("Number = {0}", Number);
return 0;
}
}
This would produce:
Number = 15.25
Number = 128.44
Practical Learning: Passing Arguments By Reference |
|
- To pass arguments by reference, change the Cylinder.cs file as follows:
using System;
namespace Geometry1
{
class Cylinder
{
public void GetRadius(ref double rad)
{
Console.Write("Radius: ");
rad = double.Parse(Console.ReadLine());
}
public void GetHeight(out double h)
{
Console.Write("Height: ");
h = double.Parse(Console.ReadLine());
}
public double CalculateBaseArea(double rad)
{
return rad * rad * Math.PI;
}
public double CalculateLateralArea(double rad, double hgt)
{
return 2 * Math.PI * rad * hgt;
}
public double CalculateTotalArea(double rad, double hgt)
{
return 2 * Math.PI * rad * (hgt + rad);
}
public double CalculateVolume(double rad, double hgt)
{
return Math.PI * rad * rad * hgt;
}
public void Process()
{
double Radius = 0.00;
double Height = 0.00;
double BaseArea;
double LateralArea;
double TotalArea;
double Volume;
Console.WriteLine("Enter the dimensions of the cylinder");
GetRadius(ref Radius);
GetHeight(out Height);
BaseArea = CalculateBaseArea(Radius);
LateralArea = CalculateLateralArea(Radius, Height);
TotalArea = CalculateTotalArea(Radius, Height);
Volume = CalculateVolume(Radius, Height);
Console.WriteLine("\nCylinder Characteristics");
Console.WriteLine("Radius: {0}", Radius);
Console.WriteLine("Height: {0}", Height);
Console.WriteLine("Base: {0:F}", BaseArea);
Console.WriteLine("Lateral: {0:F}", LateralArea);
Console.WriteLine("Total: {0:F}", TotalArea);
Console.WriteLine("Volume: {0:F}", Volume);
}
}
}
|
- Execute the application to test it. Here is an example:
Enter the dimensions of the cylinder
Radius: 24.55
Height: 20.85
Cylinder Characteristics
Radius: 24.55
Height: 20.85
Base: 1893.45
Lateral: 3216.16
Total: 7003.05
Volume: 39478.34
|
- Close the DOS window
A typical program involves a great deal of names that represent variables and
methods of various kinds. The compiler does not allow two variables to have the same name in the same
method. Although two
methods should have unique names in the same program, a class can have different
methods with the same name if you follow some rules. The ability to have various
methods with the same name in the same program is referred to as method overloading.
To perform overloading, the
methods must have different numbers or different type(s) of arguments.
The moment of inertia is the ability of a beam to resist bending.
It is calculated with regard to the cross section of the beam. Because it depends on the
type of section of the beam, its calculation also depends on the type of section of the
beam. In this exercise, we will review different formulas used to calculate the moment of
inertia. Since this exercise is for demonstration purposes, you do not need to be a
Science Engineering major to understand it.
|
|