Tutorial by Examples

A function is defined by at least its return type and an unique name. void say_hello () { print ("Hello, world!\n"); } Then, to call it just use the name of the function followed by a parenthese. say_hello (); Functions can also have parameters between the parentheses, d...
Parameters can be marked as optional by giving them a default value. Optional parameters can be omitted when calling the function. string greet (string name, string language = "English") { if (language == "English") { return @"Hello, $name!"; } else ...
Value types (structures and enumerations) are passed by value to functions: a copy will be given to the function, not a reference to the variable. So the following function won't do anything. void add_three (int x) { x += 3; } int a = 39; add_three (a); assert (a == 39); // a is still 39...
You can assert that parameters have certain values with requires. int fib (int i) requires (i > 0) { if (i == 1) { return i; } else { return fib (i - 1) + fib (i - 2); } } fib (-1); You won't get any error during the compilation, but you'll get an error wh...
int sum (int x, ...) { int result = x; va_list list = va_list (); for (int? y = list.arg<int?> (); y != null; y = list.arg<int?> ()) { result += y; } return result; } int a = sum (1, 2, 3, 36); With this function, you can pass as many int as you w...

Page 1 of 1