Declaring classes and methods
We’ve now seen that you can declare variables and execute simple operations such as incrementing a variable, assigning values, and invoking common methods such as Console.WriteLine
.
If these capabilities were the extent of Polyglot Notebooks, it would still be an impressive tool, but perhaps limited in what it could achieve. Thankfully, we’re able to build much more complex notebooks by defining our own classes and methods inside of a notebook.
Declaring methods
Let’s start by adding a method. Add a new code cell that declares a CountOddNumbers
method using the following code:
// This method counts all odd numbers leading up to the number // For example, 5 would return 3 counting the numbers 1, 3, and 5. int CountOddNumbers(int input) { double divResult = input / 2.0; return (int)Math.Ceiling(divResult); }
This code uses division to get a remainder from dividing by two and...