Difference between revisions of "Functions - 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
 
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.
+
A function is a module of code that performs a given task on data passed to it as parameters, this block will then return a value back. 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.
 +
 +
==Example of a function==
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
public Warren(int Variability, int rabbitCount)
+
    private double CalculateRandomValue(int BaseValue, int Variability)
{
 
    this.Variability = Variability;
 
    this.RabbitCount = rabbitCount;
 
    Rabbits = new Rabbit[MaxRabbitsInWarren];
 
    for (int r = 0; r < RabbitCount; r++)
 
 
     {
 
     {
      Rabbits[r] = new Rabbit(Variability);
+
      return BaseValue - (BaseValue * Variability / 100) + (BaseValue * Rnd.Next(0, (Variability * 2) + 1) / 100);
 
     }
 
     }
}
 
 
</syntaxhighlight>
 
</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.
+
As you can see the function above is from the Warren class, it specifies the data type (after private) and must have a return command to pass the value back.

Latest revision as of 06:28, 26 May 2017

A function is a module of code that performs a given task on data passed to it as parameters, this block will then return a value back. 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.

Example of a function

    private double CalculateRandomValue(int BaseValue, int Variability)
    {
      return BaseValue - (BaseValue * Variability / 100) + (BaseValue * Rnd.Next(0, (Variability * 2) + 1) / 100);
    }

As you can see the function above is from the Warren class, it specifies the data type (after private) and must have a return command to pass the value back.