C# Functions
C# Functions
<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
A function that does not return any value specifies void type
as a return type. In the following example, a function is
created without return type.
using System;
namespace FunctionExample
{
class Program
{
// User defined function without return type
public void Show() // No Parameter
{
Console.WriteLine("This is non parameterized
function");
// No return statement
}
// Main function, execution entry point of the
program
static void Main(string[] args)
{
Program program = new Program(); // Creating
Object
program.Show(); // Calling Function
}
}
}
Output:
**************************************************************
*************
using System;
namespace FunctionExample
{
class Program
1
{
// User defined function without return type
public void Show(string message)
{
Console.WriteLine("Hello " + message);
// No return statement
}
// Main function, execution entry point of the
program
static void Main(string[] args)
{
Program program = new Program(); // Creating
Object
program.Show("Rahul Kumar"); // Calling
Function
}
}
}
Output:
**************************************************************
*************
using System;
namespace FunctionExample
{
class Program
{
// User defined function
public string Show(string message)
{
Console.WriteLine("Inside Show Function");
return message;
}
// Main function, execution entry point of the
program
static void Main(string[] args)
{
Program program = new Program();
string message = program.Show("Rahul Kumar");
Console.WriteLine("Hello "+message);
}
}
}
2
Output:
**************************************************************
*************
4. using System;
namespace FunctionDemo
{
class Program
{
static void Main(string[] args)
{
int number = 25;
double squareRoot = Math.Sqrt(number);
Console.WriteLine($"Square Root of {number} is
{squareRoot}");
Console.ReadKey();
}
}
}
Output: Square Root of 25 is 5
**************************************************************
*************
using System;
class Program
3
// Don’t provide any return statement
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
**************************************************************
*************
6. 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
return sq;
4
public static void Main(string[] args)
int rslt = pr.square( 2); //Calling the method and assigning the value to an integer
type
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