Module 6 - Functions
Module 6 - Functions
Functions
By
SRIRAM . B
Overview
Functions
Defining and Using Functions
Function Arguments / Parameters
Value Parameter
Reference Parameter
Out Parameters
Parameter Arrays
Return Values
Variable Scope
Functions
Functions are the building blocks of a C-Sharp program.
Arguments or parameters of a function are the data that the function receives when
called from another function.
We have any number of parameter, each with type and name. The parameters are
separated by the comma. Each of these parameters are accessible from code
within the function as variable.
The ref keyword on a method parameter causes a method to refer to the same
variable that was passed in to the method. Any changes made to the parameter in
the method will be reflected in that variable when control passes back to the calling
method.
Example 1 – Reference Parameter
using System;
class Reference_parameter
{
// var1 refers to var2
public static void increment(ref int var1)
{
var1++;
}
public static void Main()
{
Example 1 – Reference Parameter..
int var2=10;
Console.WriteLine(“Value of var2 before function
call {0}”,var2);
//Function call by reference
increment(ref var2);
Console.WriteLine(“Value of var2 after function call
{0}”,var2);
}
}
The output of the code is:
Value of var2 before function call 10
Value of var2 after function call 11
Output Parameter
Same way as ref keyword, the output parameter using the out keyword.
We can use unassigned variable using output parameter where as this is not
a=b=10;
// The function call transfers the value of
result to res
add(a,b,out res);
Console.WriteLine("Result is {0}",res);
}
}
The output of the code is:
Result is 20
Parameter Arrays
Special Parameter for a function
It lets you to specify a method parameter that takes an argument where the number
keyword.
using System;
class Array_parameter
{
public static void func(params int[] arr)
{
Console.WriteLine(“No. of elements in the
array is{0}”,arr.Length);
}
public static void Main()
{
Example 1 – Parameter Arrays
func();
func(1);
func(1,2);
func(1,2,3);
}
}
The output of the code is:
No. of elements in the array is 0
No. of elements in the array is 1
No. of elements in the array is 2
No. of elements in the array is 3
Return Values
A function can return a single value to a calling program which can be trapped.
The data type of the return value has to specified when defining the function.
The type void is used when the function does not return any value.
Example 1 – Return Values
using System;
class Return_value
{
public static int add(int var1,int var2)
{
int result=var1+var2;
return result;
}
Example 1 – Return Values
Variables whose scope covers a single function in this way are known as local
variable. It is also possible to have global variables, whose scope covers multiple
functions.
We can use either static or const keyword for global variable of this form. To
modify the value of the global variable we need to use the static, as const prohibits
the change in the value of variable. Also const variables are implicitly static.
Example – Variable Scope (Local & Global)
using System;
namespace c2
{
public class varscope1
{
static string name;
public static void Func()
{
string name = "String defined in Func()";
Console.WriteLine("Now in Func()");
Console.WriteLine("Local Name={0}", name);
Console.WriteLine("Global Name={0}", varscope1.name);
}
Example – Variable Scope (Local & Global)