0% found this document useful (0 votes)
20 views

Functions in C Sharp Lec 2

The document discusses functions and methods in C# and how they differ from VB.NET. It covers defining functions with parameters and return types, passing parameters by value and reference, and creating functions with optional parameters.

Uploaded by

balouch86013
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Functions in C Sharp Lec 2

The document discusses functions and methods in C# and how they differ from VB.NET. It covers defining functions with parameters and return types, passing parameters by value and reference, and creating functions with optional parameters.

Uploaded by

balouch86013
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C Sharp

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.

In C#, functions have the following syntax:

<Access Specifiers> <return type> <name of the function>(< function parameters>)

<function code>

return;

1. Functions with Parameters


2. Functions returning Value
3. Functions with “Optional” Parameters
To have optional parameters(s) in the method or function, we need to initialize the
parameter in the signature such as shown in
C Sharp

Passing Parameters to a Method


A Function With A Parameter But No Return Type
Let’s create a function by providing some parameters without returning anything.
using System;
class Program
{
// function without any return type declaration
public void square(int nmbr)
{
int sq = nmbr * nmbr;
Console.WriteLine("Square of the given number is " + sq);

// Don’t provide any return statement


}

public static void Main(string[] args)


{
Program pr = new Program(); // Creating a class Object
pr.square( 2); //calling the method
}
}
In the program above, we created a function “square” by providing an integer
parameter i.e. “nmbr”. Then inside the parenthesis, we have defined the code snippet
without providing any return type to the function. In the end, we created a class object
and called the “square” function by passing an integer value as an argument.

Output
Square of the given number is 4

Let’s have a look at another example, to clear the things up.

A Function With Both Parameter And A Return Type


Let’s make some changes to the above example and add a return type.
using System;
class Program
{
// function with integer return type declaration
public int square(int nmbr)
{
int sq = nmbr * nmbr;
// Lets provide a return statement
return sq;
C Sharp

public static void Main(string[] args)


{
Program pr = new Program(); // Creating a class Object
int rslt = pr.square( 2); //Calling the method and assigning the value to an integer t
Console.WriteLine("Square of the given number is "+ rslt); //Printing the result
}
}
In the above program, we created a function “square” by providing an integer
parameter i.e. “nmbr” and a return type integer. Then inside the parenthesis, we have
defined the code snippet followed by a return statement.

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

A Function With Optional Parameters


C Sharp

When method with parameters is called, you need to pass the


parameters to the method. There are three ways that parameters
can be passed to a method

Sr.No. Mechanism & Description

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);
}

public static void Main(string[] args)


{
C Sharp

int nmbr = 2; // Value assigned before calling function


Program pr = new Program(); // Creating a class Object

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);
}

public static void Main(string[] args)


{
int nmbr = 2; // Value assigned before calling function
Program pr = new Program(); // Creating a class Object

MessageBox.Show( ("The given number is " + nmbr); //printing the value


pr.square( ref nmbr); //calling by reference using ref keyword

}
}

So, if we execute the program we will find the following output:


Square of the given number is 4
The given number is 4
C Sharp

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.

You might also like