C# Function Syntax
<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
1.C# Function: using no parameter and return type
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:
This is non parameterized function
**************************************************************
*************
2.C# Function: using parameter but no return type
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:
Hello Rahul Kumar
**************************************************************
*************
3. C# Function: using parameter and return type
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:
Inside Show Function
Hello Rahul Kumar
**************************************************************
*************
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
**************************************************************
*************
5. 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);
3
// 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
**************************************************************
*************
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
// function with integer return type declaration
public int square(int nmbr)
int sq = nmbr * nmbr;
// Lets provide a return statement
return sq;
4
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
type
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
Square of the given number is 4