Difference between revisions of "Functions - 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which pe...")
 
Line 1: Line 1:
 +
A function is a module of code that performs a given task on data passed to it as parameters, this block can return or alter the data passed to it depending on how it's written. Furthermore functions can be called throughout your program and even in other functions once they are written.
 +
 
In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which performs some operation but does not return a value. Most programming languages come with a prewritten set of functions that are kept in a library.
 
In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which performs some operation but does not return a value. Most programming languages come with a prewritten set of functions that are kept in a library.
 +
 +
<syntaxhighlight lang="csharp">
 +
public Warren(int Variability, int rabbitCount)
 +
{
 +
    this.Variability = Variability;
 +
    this.RabbitCount = rabbitCount;
 +
    Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
    for (int r = 0; r < RabbitCount; r++)
 +
    {
 +
      Rabbits[r] = new Rabbit(Variability);
 +
    }
 +
}
 +
</syntaxhighlight>
 +
 +
As you can see the function Warren, although it doesn't return any data, you pass it two variables between the parenthesis. Then using those variables it will create an array for the Rabbits with the size set as the maximum amount of rabbits in a warren. Then it will loop through the amount of rabbits and on each item of the Rabbits array it will add a Variability to it.

Revision as of 15:41, 13 February 2017

A function is a module of code that performs a given task on data passed to it as parameters, this block can return or alter the data passed to it depending on how it's written. Furthermore functions can be called throughout your program and even in other functions once they are written.

In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which performs some operation but does not return a value. Most programming languages come with a prewritten set of functions that are kept in a library.

public Warren(int Variability, int rabbitCount)
{
    this.Variability = Variability;
    this.RabbitCount = rabbitCount;
    Rabbits = new Rabbit[MaxRabbitsInWarren];
    for (int r = 0; r < RabbitCount; r++)
    {
       Rabbits[r] = new Rabbit(Variability);
    }
}

As you can see the function Warren, although it doesn't return any data, you pass it two variables between the parenthesis. Then using those variables it will create an array for the Rabbits with the size set as the maximum amount of rabbits in a warren. Then it will loop through the amount of rabbits and on each item of the Rabbits array it will add a Variability to it.