A function is a block of statements that perform a coherent
task for you. Each C# class can contain one or more functions. Execution always
begins with Main() function present in the class that is marked as startup
object.
For any function, we will pass data to it by giving its
arguments and the function will pass data back using a return value. We can
have any number of arguments/parameters but only one return value to a
function.
Defining a function:
<security level[takes default value if not specified]> <return data type[if not return value then we need to write “void”]> <function_name>(declaration of 1 or more parameters)
{
We can use the parameters defined above as local variables here,and return a value
}
Example:
//Defining a function:
static int Kiran(int a,int b)//static keyword here is required as we are not creating object.We will discuss in OOPS.
{
int c; //this is local variable to Kiran function and only available here.
c=a+b;
return c;//to return back the variable c value to function call.
}
//calling a function
int d=Kiran(4,5);
In C# there are 3 ways to pass parameters to a function. As
value, reference and out parameters.
In Call by Value,
value of each of the arguments in calling function is copied into corresponding
formal arguments of the called function. The changes made to the formal
arguments in the called function have no effect on the values of actual
arguments in the calling function.
//Calling a function by Value
using System;
namespace CallingByReferenceandValue
{
class CallingByReferenceandValue
{
static void Main(string[] args)
{
int a=10,b=20;
Swap(a,b);
Console.WriteLine("a={0} b={1}",a,b);
Swapp(a,b);
Console.WriteLine("a={0} b={1}",a,b);
}
static void Swap(int x,int y)//Calling a function by value.
{
int t;
t=x;
x=y;
y=t;
Console.WriteLine("x={0} y={1}",x,y);
}
static void Swapp(ref int x,ref int y)//Calling a function by reference.
{
int t;
t=x;
x=y;
y=t;
Console.WriteLine("x={0} y={1}",x,y);
}
}
}
The output of above program would be:
x=20 y=10
a=10 b=20
x=20 y=10
a=20 b=10
x=20 y=10
a=10 b=20
x=20 y=10
a=20 b=10
If you see, a and b values are unchanged even after
exchanging the values of x and y in first Swap function/method which is Call by Value.But it changing with
second method Swapp which is Call by
Reference.
The third way is used when we want to return multiple values
from a function. à
OutParameters.
//OutParameters
using System;
namespace OutParameters
{
class OutParameters
{
static void Main(string[] args)
{
int a=10,b=20,c=30;
int s,p;
SumProd(a,b,c,out s,out p);
Console.WriteLine("Sum={0} Prod={1}",s,p);
}
static void SumProd(int x,int y,int z,out int ss,out int pp)
{
ss=x+y+z;
pp=x*y*z;
}
}
}
Note: Once we define output parameters it’s mandatory to
assign some value.We will get error if don’t.
We can use reference variables instead of output parameters
but then it is necessary to initialize those variables in Main() i.e., calling
function.
Function Overloading:
We can define multiple functions with same name [ But with
different parameter (both in type or number).]
//Function Overloading
using System;
namespace FunctionOverloading
{
class FunctionOverload
{
static void Main(string[] args)
{
int i=-25,j;
long l=-100000,m;
double d=-12.34,e;
j=Abs(i);
m=Abs(l);
e=Abs(d);
Console.WriteLine("j={0} m={1} e={2}",j,m,e);}
}
static int Abs(int ii)
{
return(ii>0?ii:ii*-1);
}
static long Abs(long ii)
{
return(ii>0?ii:ii*-1);
}
static double Abs(double ii)
{
return(ii>0?ii:ii*-1);
}
}
}
Recursion:
A function is recursive if a statement within the body of a
function calls the same function.
Below is the example for calculating factorial using
recursive function.
using System;
namespace RecursiveFunction
{
class RecursiveFunction
{
static void Main(string[] args)
{
int a,fact;
Console.WriteLine("Enter any number");
a=Convert.ToInt32(Console.ReadLine());
fact=Rec(a);
Console.WriteLine("Factorial value="+fact);}
}
static int Rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*Rec(x-1);
return(f);
}
}
}











.jpg)