Vous êtes sur la page 1sur 28

Module 6:

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.

 A number of C-Sharp program statements can be grouped together to form a


function.

 A function can be invoked from other parts of a program.

 Functions help in the easy maintenance of code called Reusable.

 Functions make a program more readable.

 Any sequence of instructions that is repeated in a program can be grouped


together to form a function.

 Consist of Parameter and Return Values , collectively defined as signature


Example 1 - Function
using System;
class function
{
//Declaring functions
public static void add()
{
//Function body
Console.WriteLine("In add method");
}
public static void Main()
{
//Invoking the function
add();
Console.WriteLine(“After function call”);
}
}
Example 1 - Function
Output of the code is :
In add method
After function call
Function Arguments / Parameters

 Arguments or parameters of a function are the data that the function receives when
called from another function.

 It is not always necessary for a function to have arguments.

 The function add() can be modified to take arguments as follows:


public static void add(int a, int b)
{
int sum=a+b;
Console.WriteLine(“Sum is :{0}”, sum);
}
Function Arguments / Parameters
 When the function accepts parameter we have to define the following :-

 A list of parameter accepted by by a function in its definition, along with the


types of those parameters.
 A matching list of parameters in each functions call.

 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 parameters in functions can be the following:


 Value parameter
 Reference parameter
 Output parameter
 Parameter arrays
Value Parameter

 When we have used parameters we have passed a value in to a variable used by


the function. Any changes made to this variable in the function have no effect on
the parameters specified in the function.
Example 1 – Value Parameter
class Value_parameter
{
int var2=10;
Console.WriteLine(“Value of var2 before function
call {0}”,var2);
//Function call by value
increment(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 10
Reference Parameter
 To pass the parameter by reference use the ref keyword.

 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

possible using ref keyword.


Example - Out Parameter
using System;
class Output_parameter
{
public static void add(int var1,int var2, out int result)
{
result=var1+var2;
}
public static void Main()
{
int a,b,res;
Example - Out Parameter

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

 Defined using “params” keyword

 It lets you to specify a method parameter that takes an argument where the number

of arguments is variable, no additional parameters are permitted after the “params”

keyword.

 Only one “params” keyword is permitted in the method declaration.


Example 1 – Parameter Arrays

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 return keyword is used to return a value to a calling 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

public static void Main()


{
int a,b,res;
a=b=10;
//The return value is stored in res
res=add(a,b);
Console.WriteLine("Result is {0}",res);
}
}
Variable Scope

 A variable is said to have scope from where it is accessible.

 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)

static void Main(string[] arg)


{
string name = "String defined in Main()";
varscope1.name = "Global String";
Func();
Console.WriteLine("\nNow in Main()");
Console.WriteLine("Local Name={0}", name);
Console.WriteLine("Global Name={0}", varscope1.name);
}
}
}
Functions Flashback
 Functions
 Defining and Using Functions
 Function Arguments / Parameters
 Value Parameter
 Reference Parameter
 Out Parameters
 Parameter Arrays
 Return Values
 Variable Scope
Session Ends
Exercise
Relax

Vous aimerez peut-être aussi