Difference between revisions of "Parameter Passing - 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "dibs")
 
(From Simulation)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
dibs
+
Parameter passing is inserting values into methods or calculations using a declared variable, rather than the raw data (strings, numbers, bools, etc.).
 +
This can be done in multiple ways, but the most often used (in the skeleton program) will be:
 +
<syntaxhighlight lang="csharp">
 +
int a = 10;
 +
string b = "yes";
 +
 
 +
Console.WriteLine(a);
 +
Console.WriteLine(b);
 +
 
 +
</syntaxhighlight>
 +
The parameters a and b are passed to the WriteLines.
 +
 
 +
==Example From Simulation==
 +
The following lines are in the Simulation class, and they call a method called InputCoordinate with the parameter value of 'x' and then 'y'.
 +
 
 +
<syntaxhighlight lang="csharp">
 +
if (menuOption == 4)
 +
        {
 +
          x = InputCoordinate('x');
 +
          y = InputCoordinate('y');
 +
        ...
 +
        }
 +
</syntaxhighlight>
 +
 
 +
The code below is the InputCoordinate method called above. The method expects to be passed a char data type, and within the method this will be called Coordinatename.
 +
 
 +
<syntaxhighlight lang="csharp">
 +
    private int InputCoordinate(char Coordinatename)
 +
    {
 +
      int Coordinate;
 +
      Console.Write("  Input " + Coordinatename + " coordinate: ");
 +
      Coordinate = Convert.ToInt32(Console.ReadLine());
 +
      return Coordinate;
 +
 
 +
</syntaxhighlight>
 +
 
 +
This is also a function because InputCoordinate returns a value back (an int).

Latest revision as of 06:35, 26 May 2017

Parameter passing is inserting values into methods or calculations using a declared variable, rather than the raw data (strings, numbers, bools, etc.). This can be done in multiple ways, but the most often used (in the skeleton program) will be:

 int a = 10;
 string b = "yes";

 Console.WriteLine(a);
 Console.WriteLine(b);

The parameters a and b are passed to the WriteLines.

Example From Simulation

The following lines are in the Simulation class, and they call a method called InputCoordinate with the parameter value of 'x' and then 'y'.

 if (menuOption == 4)
        {
          x = InputCoordinate('x');
          y = InputCoordinate('y');
        ...
        }

The code below is the InputCoordinate method called above. The method expects to be passed a char data type, and within the method this will be called Coordinatename.

    private int InputCoordinate(char Coordinatename)
    {
      int Coordinate;
      Console.Write("  Input " + Coordinatename + " coordinate: ");
      Coordinate = Convert.ToInt32(Console.ReadLine());
      return Coordinate;

This is also a function because InputCoordinate returns a value back (an int).