Functions in C Sharp Lec 2
Functions in C Sharp Lec 2
Functions and Methods are exactly same in C# But in VB. NET Function
return a value while Method doesn’t return a value.
The main reason for using Functions and Methods are
1. Avoid repeating code for easy maintenance
2. Divide a big function or method into small code
3. Code reuse in different programs.
Introduction To Functions In C#
In C# a function is defined as a technique of wrapping code to perform a certain task.
The function is a member of the class. It is quite the same as a method and sometimes
both the terms are used interchangeably. But there are few basic differences between
methods and functions.
A function allows the programmers to enclose a piece of code and then call that part
of the code from another part of the program. It is quite useful when you need to run
the same code from different places.
<function code>
return;
Output
Square of the given number is 4
Inside the main function, we created a class object and called the “square” function by
passing an integer value as an argument. As there is a return type associated, we then
stored the function in an integer variable. In the end, we printed the result.
Output
S quare of the given number is 4
Value parameters
This method copies the actual value of an argument into the formal parameter
1
of the function. In this case, changes made to the parameter inside the function
have no effect on the argument.
Reference parameters
This method copies the reference to the memory location of an argument into
2
the formal parameter. This means that changes made to the parameter affect
the argument.
Output parameters
3
This method helps in returning more than one value.
As we discussed in our previously, Access specifies, Parameters and return types are
optional. Let’s create functions with different options.
C# Call By Value
In C# programming language, when we call a function, then it takes a parameter from
the main function using the class object. Then the class object inside the main
function will copy the function to parameter values. When we use call by value, even
if some changes occur within the method that change will not be transferred to the
original variable.
Example:
using System;
class Program
{
public void square(int nmbr)
{
nmbr = nmbr * nmbr;
// Lets provide a return statement
MessageBox.Show( ("Square of the given number is " + nmbr);
}
pr.square( nmbr); //calling the method and assigning the defined integer
MessageBox.Show("The given number is " + nmbr); //printing the value
}
}
So, if we execute the above program we will find the following output:
Square of the given number is 4
The given number is 2
Explanation
In the above example, we defined an integer variable “nmbr” with a value of 2. Then
we called the square function by passing the variable as an argument. Hence, the
variable that we passed changed into a multiplication of itself (due to operation of the
function) and printed the result.
In the main function at the end, we print the variable that we defined earlier. As we
can see, there has been no change in the variable value of the function (where it is
defined) but it did change when we passed it as an argument for another function.
As we discussed earlier when we call by value any change that will occur to the
variable in a method will not be transferred to the original variable. Hence, when we
performed the print operation on the variable, it still gives us the previously defined
output.
C Sharp
C# Call By Reference
C# offers a “ref” keyword for passing an argument as a reference type for a function. Unlike call by the
value, it doesn’t pass the variable to the function after creating a copy of the variable.
It passes the reference of the original value to the function, hence any change that occurs in the
referenced value is permanent and is reflected in the original value as well.
Let’s use the same example as earlier but instead of using call by the value we
will be using call by reference:
using System;
class Program
{
public void square(ref int nmbr)
{
nmbr = nmbr * nmbr;
// Lets provide a return statement
MessageBox.Show("Square of the given number is " + nmbr);
}
}
}
Explanation
In the above example, we defined an integer variable “nmbr” with a value of 2. Then
we called the square function by passing the variable as an argument. So, the variable
that we passed changed into a multiplication of itself (due to operation inside the
function) and printed the result i.e. 4.
In the main function at the end, we print the variable that we defined earlier. As we
can see that there have been changes in the variable value in the function where it was
referenced and operated on. As the function performed the operation and variable
value changed to 4, the same is reflected in console output.
As we discussed earlier when we call by reference any change that will occur to the
variable in a method will be transferred to the original variable. Hence, when we
performed the print operation on the variable, it will print the current output i.e. 4.