More on Functions and Subroutines

Let's talk about passing values ByRef.
        private void newtonlaw(ref float force, ref float mass, ref float acceleration) {
            if (force == 0f) {
                force = mass * acceleration;
            } else if (mass == 0f) {
                mass = force / acceleration;
            } else if (acceleration == 0f) {
                acceleration = force / mass;
        }// End of newtonlaw subroutine.
The equivalent to passing arguments ByRef when they are normally passed ByVal is by using the ref keyword. All you have to do is place ref in front of the applicable code. The code above will find the first argument that is zero and solve for it using the equation F = ma. Since all three of the arguments are passed as ref floats (ByRef), we can modify the associated variable that was used to call this subroutine. However, there is another thing that is important when dealing with passing ref datatypes into subroutines:
        private void btn9_Click(object sender, EventArgs e) {
            float F = 0f, M = 6.25f, A = 8f;
            newtonlaw(ref F, ref M, ref A);
        } // End of btn 9's click procedure.
Notice that we now have to specify ref in front of any variable that is passed into the subroutine where a ref is expected. Don't forget that the refs must match up, otherwise, you'll get an error explicitly stating that you need to put ref in front of the variable. I don't see why Visual Studio wouldn't just put one there, but I suppose it's best for C# guys to type out all of the obvious mistakes they made. Or, perhaps, it's there to help the programmer realize that the function is expecting a ref variable to be passed in. Well, maybe the reverse is true? If you have a subroutine that doesn't expect a ref variable, but you pass in a variable ref, does that count as passing the variable as a reference? The answer is, similarly, no... instead netting you another error that essentially tells you to get rid of the ref in front the variable. So, the quick and sweet answer is to put ref in front of the types for the subroutine declaration, and ref in front of the variables passed in to a subroutine that expects ref to be passed.
Another keyword that provides functionality similar to passing ByRef is the out keyword:
        private void centripetalforce(out float forcecent, float mass, float speed, float radius) {
            forcecent = mass * speed * speed / radius;
        }  // Calculates centripetal force and returns the output out of the first argument.
The out keyword is different from the ref keyword in the following way: the subroutine must assign a value to any 'out' variable. Essentially, this functionality is very similar to a regular function declaration with the ability of returning multiple independent values at once (although, you lose the ability to nest calls to functions using 'out' variables. Calling the centripetalforce function is done the same way you'd expect (based on the results of ref).
        private void btn9_Click(object sender, EventArgs e) {
            float F, M = 6.25f, A = 0f, S = 12f, R = 2f;
            centripetalforce(out F, M, S, R); // Calculate the force required for circular motion.
            newtonlaw(ref F, ref M, ref A); // Calculate the resultant acceleration.
        } // End of btn 9's click procedure.
Simply place out in front of the variable that is expected to be passed with out. You cannot mix out with a regular parameter or a ref parameter - this just gets you errors that tell you exactly what you need to put in front of each variable. But, these two keywords provide functionality equivalent to that of ByRef. Note that, when passing a ref variable, the variable is expected to be initialized, but when passing an out variable, the variable is not expected to be initialized.

Difference Summary

End of a code lineNew line;
Comment'// or /* ... */
32-bit integerIntegerint
32-bit floating pointSinglefloat
Letter literal for doubleRD
Letter literal for decimalDM
Letter literal for shortSno equivalent
Division// - one operand must be a floating point number
Integer Division\/ - both operands must be integers
Modulus (Division Remainder)Mod%
If blockIf condition Then ... End Ifif (condition) { ... }
If Else blockIf condition Then ... Else ... End Ifif (condition) { ... } else { ... }
If ElseIfIf condition Then ... ElseIf condition Then ... End Ifif (condition) { ... } else if (condition) { ... }
Check for equality===
Check for inequality<>!=
Specify index to arrayarr(...)arr[...]
Instantiate an array by sizeNew Sometype(highestindex) {}new sometype[numberelements]
Instantiate an array elementsNew Sometype() {..., ..., ...}new sometype[] {..., ..., ...}
Increment variable by oneno equivalent++variable or variable++
Decrement variable by oneno equivalent--variable or variable--
Literal character"A"c'A'
Boolean 'and' operatorAndAlso&&
Boolean 'or' operatorOrElse||
Boolean 'not' operatorNot (followed by boolean)!
Binary 'and' operatorAnd&
Binary 'or' operatorOr|
Binary 'not' operatorNot (followed by integer)~
Binary 'xor' operatorXor^
For LoopFor variable = initial To final Step value ... Nextfor (initial ; condition ; statement on loop) { ... }
While LoopWhile condition ... End Whilewhile (condition) { ... }
Do LoopDo [Until/While condition] ... Loop [Until/While condition]do { ... } loop while (condition)
Reference to the own classMethis
Escape sequence for a quote in a string""\"
Escape sequence for a backslash\\\
String concatenation&+
FunctionScopeword Function fxnname(arguments) As returntype ... End Functionscopeword returntype fxnname(arguments) { ... }
SubScopeword Sub subname(arguments) ... End Subscopeword void subname(arguments) { ... }
Pass by valueByVal(this is the default)
Pass by referenceByRefout as output only, otherwise use ref