find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

What is function and how to work Functions in C#

What is function and how  to work Functions in C#

Function is a block of code that has does something and then returns the value.The function is used to execute statements in the code block.A function consists of the following components:
 
 
 Function name: It is a unique name in same class name that is used to make Function call.
 
 Return type:The Function return value for specify the data type of function.
 
 Body: It is a collection of code block that contains executable statements.
 
 Access specifier: Specify function accessibility in the application.
 
 Parameters: List of arguments we can pass to the  the function during call.
 
 
C# Function Syntax
 
<AccessSpecifier><ReturnType>FunctionName(<parameters>)
{
    // function body  
    // return statement  
}
 
Let's see different types of function example in c#\

 
 C# Function: Using no argument and no return type

 
This function is not required any argument and no return any type of values.
So value specifies void type as a return type.
 
For Example:
 
using System;  
namespace FunctionExample
{
    class Program
    {
        // User defined function without return type  
        public void Exaxmple() // No Parameter  
        {
         Console.WriteLine("no argument or parameter and no return type function.");
            // No return statement  
        }
        // Main function execute the program  
        static void Main(string[] args)
        {
            Program objProgram = new Program(); // Creating Object 
            objProgram.Exaxmple(); // Calling Function here          
        }
    }
}

  C# Function:With argument(Parameter) but no return type

Output:
no argument or parameter and no return type function.

 

A function is created without parameters and return value.
 
For Example:
 
using System;  
namespace FunctionExample
{
     class Program
    {
          // User defined function without return type  
        
         public void Example(string message)
        {
            Console.WriteLine("Welcome To " + message);
            // No return statement  
        }
        // Main function execute the program  
        static void Main(string[] args)
        {
            Program objProgram = new  Program(); // Creating Object  
            objProgram.Example("findandsolve.com"); // Calling Function             
        }
    }
}
 
 
Output:
 
Welcome To findandsolve.com
 

 C# Function: using argument or parameter with return type.

For Example:
 
using System;  
namespace FunctionExample
{
   class Program
    {
        // User defined function  
        public int Result(int firstNumber,int secondNumber)
        {
            Console.WriteLine("argument with return type function example");
            return firstNumber+secondNumber;
        }
         // Main function execute the program  
        static void Main(string[] args)
        {
            Program objProgram = new Program();
            int restult = objProgram.Result(10,20);
            Console.WriteLine("The Result is :" + restult);
        }
    }
}
 
Output:
 
argument with return type function example
The Result is:30

 

C# Function: No argument but return type.

 
This function normally use in success,failed,update message something like that.Also use when I have save some data in database and change the another table status globally.
 
For Example:
 
using System;  
namespace FunctionExample
{
   class Program
    {
        // User defined function  
        public string SuccessMessage()
        {
            return "Your Request is Success!..";
        }
        // Main function, execution entry point of the program  
        static void Main(string[] args)
        {
            Program objProgram = new Program();
            string message = objProgram.SuccessMessage();
            Console.WriteLine(message);
        }
    }
}
 
 
Output:
Your Request is Success!..
 

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response