Parameters - Global Variables

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

When a variable needs to be passed into a subroutine, this is done through the use of parameters. The subroutine can then use these parameters to execute its code. Parameters should be written inside the subroutine brackets, along with their variable type. In addition, multiple parameters should be separated by commas (",").

Argument Scope

Variables are only accessible within the scope for which they are declared. A variable declared within a method is only available within that method. The same also goes for other defined parts of the program, for example if a variable is declared within a loop it only exists within the loop and can't be accessed outside of the loop.

Value Parameters

parameters passed to a function are passed as copies of their actual values and hence values sent and used as arguments by method are from a memory point of view different to the values actually passed. Because of this any changes made to arguments passed to a method have no affect on the value of the actual variable they're passed from, for example:

 1 class Program {
 2     public static void DoSomething(int value) {
 3         value++;
 4     }
 5 
 6     public static void Main(String[] Args) {
 7         int test = 1;
 8         Console.WriteLine(test); // Outputs 1;
 9 
10         DoSomething(test);
11         Console.WriteLine(test); // Outputs 1;
12 
13         test++;
14         Console.WriteLine(test); // Outputs 2;
15     }
16 }

As the example above dictates Any from the moment the integer test1 is passed to the method DoSomething the argument used by the method is intrinsically different to the variable passed to it. There are two way to circumvent this problem and attempt to save changes made by a method to a variable.

Global Variables

One Method would be to declare the variable in a more globally accessible scope in relation to the two methods and edit them referencing directly to this variable declaration. Variables declared within the class Program but not with in a method are global, for Example:

 1 class Program {
 2     public static void DoSomething() { 
 3        // int no longer passed as argument
 4         testInt++;
 5     }
 6 
 7     public static void Main(String[] Args) {
 8         testInt = 1;
 9         Console.WriteLine(testInt); // Outputs 1;
10 
11         DoSomething(); // testInt not passed as arg
12         Console.WriteLine(testInt); // Outputs 2;
13 
14         testInt++;
15         Console.WriteLine(testInt); // Outputs 3;
16     }
17 
18     public static int testInt; // int declared in same scope as two methods.
19 }

While this method has it's upside it's difficult to apply to an existing complex algorithm and doesn't actually involve any parameter passing, making it inefficient for the task it's trying to accomplish. On the other hand

Reference Parameters

Another option would be to use the ref keyword in the parameter portion of the method, allowing the value being passed as an argument to be passed as a reference to the memory location of the actual variable instead of as a copy of the value stored at said memory location.

 1 class Program {
 2     public static void DoSomething(ref int intTestRef) {
 3         intTestRef += 1;
 4     }
 5 
 6     public static void Main(String[] Args) {
 7         int test = 1;
 8         Console.WriteLine(test); // Outputs 1;
 9 
10         DoSomething(ref test); // passed as reference
11         Console.WriteLine(test); // Outputs 2;
12 
13         test += 1;
14         Console.WriteLine(test); // Outputs 3;
15     }
16 }

This method is the preferred solution to keeping changes to passed parameters.

Advanced Parameters

This could be useful to know for any project, but aren't necessary for your Unit 1 theory.

Unlimited Parameters

To pass as many arguments as you desire to a method, you can use the params keyword to set a method to accept as many parameters as given to it without throwing an exception, for example:

 1 class Program {
 2     public static String DoSomething(String seperator, params String[] args) {
 3         return String.Join(seperator, args);
 4     }
 5 
 6     public static void Main(String[] Args) {
 7         Console.WriteLine(DoSomething(" ", "BLARG", "BLARG2", "BLARG3")); // Outputs "BLARG BLARG2 BLARG3"
 8         Console.WriteLine(DoSomething(" ", "BLARG")); // Outputs "BLARG"
 9         Console.WriteLine(DoSomething("BLARG", " ", " ", " ")); // Outputs " BLARG BLARG "
10     }
11 }

Optional Parameters & nullable parameters

Parameters dont have to be mandatory, if required you can give a parameter a default value or mark it as nullable meaning it will be null unless something is passed to it. for example:

 1 class Program {
 2     public static String DoSomething(String value = "BLARG") { // optional Parameter value
 3         return value;
 4     }
 5 
 6     public static void Main(String[] Args) {
 7         Console.WriteLine(DoSomething()) // Outputs "BLARG"; Note No Parameter Passed
 8         Console.WriteLine(DoSomething("YOZIT")) // Outputs "YOZIT"; Note Parameter Passed
 9         Console.WriteLine(DoSomething(value : "BLARG")) // Outputs "BLARG"; Note Parameter Passed as named argument
10     }
11 }

this is an example of optional parameter passing, but such capabilty doesn't come without costs, for example optional parameters must be placed after mandatory parameters and must have an acceptable value, hence the following is invalid:

1 public static DoSomething(String value = "BLARG", int value2) {}

1 public static DoSomething(String value = 5) {}

To end things here I will explain nullable types, these are parameters which may or may not be used by a given method but do not have to have a set value, hence making them null. Nullable types come with a hefty HasValue attribute to let you now wether a given nullable is actually null or not, for Example:

 1 class Program {
 2     public static String DoSomething(String value? = Null) { // optional nullable Parameter
 3         if (value.HasValue) {
 4             return value.Value;
 5         } else {
 6             return "BLARG";
 7         }
 8     }
 9 
10     public static void Main(String[] Args) {
11         Console.WriteLine(DoSomething()) // Outputs "BLARG"
12         Console.WriteLine(DoSomething("YOZIT")) // Outputs "YOZIT"
13         Console.WriteLine(DoSomething(null)) // Outputs "BLARG"
14     }
15 }

Type Passing

You can also pass given types to a method for use in a variety of functions using the <TYPE> notation, in which a given type is passed as an argument and used via a given implicit identifier, for Example:

 1 class Program {
 2     public static void TypeMethod<T>(T value) {
 3        // Note T is the name of the passed type
 4         return value += 1;
 5     }
 6 
 7     public static void Main(String[] Args) {
 8         Console.WriteLine(TypeMethod<int>(5)); // Outputs "6"  // Note Type Conversion
 9         Console.WriteLine(TypeMethod<float>(5f)); // Outputs "6.0" // Note Type Conversion
10     }
11 }