Subroutines - Methods - 2017

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

A subroutine is a name given to a block of code that can be called by name to perform the operation in the subroutine without having to copy the code it contains. If the subroutine is used to return a value and not just perform an operation, it is known as a function. The subroutine can be passed by a variable parameter if the data type is specified.

 1 private void CreateNewFox()
 2     {
 3       int x, y;
 4       do
 5       {
 6         x = Rnd.Next(0, LandscapeSize);
 7         y = Rnd.Next(0, LandscapeSize);
 8       } while (Landscape[x, y].Fox != null);
 9       if (ShowDetail) {
10         Console.WriteLine("  New Fox at (" + x + "," + y + ")");
11       }
12       Landscape[x, y].Fox = new Fox(Variability);
13       FoxCount++;
14     }

This is an example of a non-function subroutine in the skeleton program.