[edit] C# Function Basics
To define a C# function you must declare it outside the main() block because the definition cannot be executed, it is merely a definition.
Example:
using System; class Program { static void print() //Function declared outside of main block { Console.WriteLine("In function"); } static void Main() //Main Block { print(); //Call print function Console.Read(); } }
Output:
In Function
- Static Keyword: We will not cover static in this section. We will cover it in the classes section. Just remember it must be included in your functions.
- void Keyword: void means that the function will not return a value
- Line 11: This is the syntax to run a function. Type the functions name followed by ();
[edit] Return Values in a C# Method
In a C# function you may want to return the result of an operation. You can return the value of a variable in a function by using the return keyword.
The program will simple print the value of pi:
Example:
using System; class Program { static double pi() //Function pi { return 3.14; //Return 3.14 } static void Main() { Console.WriteLine(pi());// calling and showing function pi Console.Read(); } }
Output:
3.14
- double keyword Simply means that the function will return a double value
- return keyword Simply returns the value to object that called it
- Line 11: Variable x called the function pi() and pi() will return 3.14
[edit] C# Function Parameters
C# methods can accept values when a variable calls it.
Example 1:
using System;
class Program
{
static int Multiply(int x) //accepts only a int value
{
return x * 2;
}
static void Main()
{
int z = 3;
Console.WriteLine(Multiply(z)); //Prints the returned value
Console.Read();
}
}
Output:
6
Example 2:
using System; class Program { static void PrintArray(int[] x) //accepts an array value { foreach (int temp in x) Console.WriteLine(temp); } static void Main() { int[] z = { 4, 45 }; double[] x = {.9, .8}; PrintArray(z); PrintArray(x); //ERROR-- Print array only takes int values Console.Read(); } }
- Line 15: Notice this program will not compile. PrintArray only takes int arrays
[edit] C# Call by Reference
In the previous sections we have been calling functions by value. Now we will learn how to call by reference. Use call by reference any chance you get because it has tremendous performance benefits. The compiler does not have to spend the time to create a temporary variable, it uses the same variable in the calling statement. So, any changes that are made to the variable are in effect through out the whole program.
Example:
using System;
class Program
{
static void AddOne(ref int x)
{
x = x + 1;
}
static void Main()
{
int z = 8;
AddOne(ref z); //variable z will be changed in function
Console.WriteLine(z);
Console.Read();
}
}
Output
9
As you can see z is passed to the function. When you call by reference it does not send the value it sends the variables address so it knows where to find it.
AddOne(z); //call by value
AddOne(ref z); //call by reference
When you are dealing with arrays it can truelly degrade the performance of your program if you use call by value. use call by reference when passing arrays.
[edit] The C# out Parameters
The out parameter is very similar to the ref keyword. You are allowed only to return one variable from a function. The out keyword gives the ability to return multiple values. You must use out in the same way as you would the ref keyword. Except in one case:
- An out variable has to be an unassigned a variable when passed to a function
Example:
using System;
class Program
{
static int AddOne(out int x)
{
int y = 90;
x = 19; //out variable must be initialized
x = x + 1;
return y + 1;
}
static void Main()
{
int z; //out variable must be unassigned be declared
Console.WriteLine(AddOne(out z));
Console.WriteLine(z);
Console.Read();
}
}
Output:
91
20
- Tip: If you assign an out variable before it is passed to a function that value will be ignored because it must be reassigned in the function
|