0% found this document useful (0 votes)
32 views5 pages

C# Functions

The document discusses C# function syntax and provides examples of functions with different parameter and return type combinations. It shows how to define functions with: 1) No parameters and no return type 2) Parameters but no return type 3) Parameters and a return type 4) Using built-in Math functions as an example 5) A function with a parameter but no return type 6) A function with both parameters and a return type

Uploaded by

Balakumar Bk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

C# Functions

The document discusses C# function syntax and provides examples of functions with different parameter and return type combinations. It shows how to define functions with: 1) No parameters and no return type 2) Parameters but no return type 3) Parameters and a return type 4) Using built-in Math functions as an example 5) A function with a parameter but no return type 6) A function with both parameters and a return type

Uploaded by

Balakumar Bk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like