Difference between revisions of "Parameters - Global Variables"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "When a variable needs to be passed into a subroutine, this is done through the use of parameters. The subroutine can then use these parameters to execute its code. Parameters ...")
 
m (Image changed to code.)
Line 1: Line 1:
 
When a variable needs to be passed into a subroutine, this is done through the use of parameters. The subroutine can then use these parameters to execute its code. Parameters should be written inside the subroutine brackets, along with their variable type. In addition, multiple parameters should be separated by commas (",").
 
When a variable needs to be passed into a subroutine, this is done through the use of parameters. The subroutine can then use these parameters to execute its code. Parameters should be written inside the subroutine brackets, along with their variable type. In addition, multiple parameters should be separated by commas (",").
  
<h3>Example</h3>
+
=== Example ===
[[File:passing variables.png]]
+
<syntaxhighlight lang="csharp" line>
 +
namespace Project
 +
{
 +
    class Program
 +
    {
 +
        static void Main(string[] args)
 +
        {
 +
            greeting("Geoff");
 +
            greeting("Bob");
 +
        }
 +
 
 +
        static void greeting(string name)
 +
        {
 +
            Console.WriteLine("Hello, " + name);
 +
        }
 +
    }
 +
}
 +
//Output:
 +
//Hello, Geoff
 +
//Hello, Bob
 +
</syntaxhighlight>

Revision as of 17:46, 16 December 2016

When a variable needs to be passed into a subroutine, this is done through the use of parameters. The subroutine can then use these parameters to execute its code. Parameters should be written inside the subroutine brackets, along with their variable type. In addition, multiple parameters should be separated by commas (",").

Example

 1 namespace Project
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             greeting("Geoff");
 8             greeting("Bob");
 9         }
10 
11         static void greeting(string name)
12         {
13             Console.WriteLine("Hello, " + name);
14         }
15     }
16 }
17 //Output:
18 //Hello, Geoff
19 //Hello, Bob