// Pass by value (in, default), reference (in/out), and reference (out)
void TestFunc(int x, ref int y, out int z)
{
x++;
y++;
z = 5;
}
int a = 1, b = 1, c; // c doesn't need initializing
TestFunc(a, ref b, out c);
Console.WriteLine("{0} {1} {2}", a, b, c); // 1 2 5
// Accept variable number of arguments
int Sum(params int[] nums)
{
int sum = 0;
foreach (int i in nums)
{
sum += i;
}
return sum;
}
int total = Sum(4, 3, 2, 1); // returns 10
// C# supports optional arguments/parameters.
// The default value is specified with the syntax '= value'.
// Optional parameters must be listed last.
void SayHello(string name, string prefix = "")
{
Console.WriteLine("Greetings, " + prefix + " " + name);
}
|
class-id Functions.
*> Pass by value (in, default), reference (in/out), and reference (out)
method-id TestFunc (x as binary-long,
reference y as binary-long,
output z as binary-long).
add 1 to x, y
move 5 to z
end method.
method-id InstMethod.
declare a as binary-long = 1
declare b as binary-long = 1
declare c as binary-long
invoke TestFunc(value a reference b output c)
*> Or
invoke self::TestFunc(a b c)
display a space b space c
declare total as binary-long
*> Commas in parameter lists are optional
set total to function sum(4 3 2 1) *> returns 10
display total
end method.
method-id main static.
declare i as binary-long
set i to MySum(1 2 3 4)
display i
declare o = new self
*> Parentheses are optional when there are no parameters.
invoke o::InstMethod
invoke SayHello("Strangelove", "Dr.")
invoke SayHello("Madonna")
end method.
*> To create a non intrinsic variable argument list function:
method-id MySum static (params nums as binary-long occurs any)
returning mysum as binary-long.
perform varying i as binary-long through nums
add i to mysum
end-perform
end method.
*> COBOL supports optional arguments/parameters.
*> The default value is specified with the syntax '= value'.
*> Optional parameters must be listed last.
method-id SayHello static (nam as string,
prefix as string = "").
display "Greetings, " prefix space nam
end method.
end class.
|
' Pass by value (in, default), reference (in/out), and reference (out)
Sub TestFunc(ByVal x As Integer,
ByRef y As Integer,
ByRef z As Integer)
x += 1
y += 1
z = 5
End Sub
Dim a = 1, b = 1, c As Integer ' c set to zero by default
TestFunc(a, b, c)
Console.WriteLine("{0} {1} {2}", a, b, c) ' 1 2 5
' Accept variable number of arguments
Function Sum(ByVal ParamArray nums As Integer()) As Integer
Sum = 0
For Each i As Integer In nums
Sum += i
Next
End Function ' Or use Return statement like C#
Dim total As Integer = Sum(4, 3, 2, 1) ' returns 10
' VB.NET supports optional arguments/parameters.
' The default value is specified with the syntax '= value',
' though optional parameters must be explicitly marked as such.
' Optional parameters must be listed last.
Sub SayHello(ByVal name As String,
Optional ByVal prefix As String = "")
Console.WriteLine("Greetings, " & prefix & " " & name)
End Sub
SayHello("Strangelove", "Dr.")
SayHello("Madonna")
|