TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Function

C# Function for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Function

Function is a block of code that has a signature. Function is used to execute statements specified in the code block. A function consists of the following components:

Function name: It is a unique name that is used to make Function call.

Return type: It is used to specify the data type of function return value.

Body: It is a block that contains executable statements.

Access specifier: It is used to specify function accessibility in the application.

Parameters: It is a list of arguments that we can pass to the function during call.

C# Function Syntax

FunctionName()
{
// function body
// return statement
}

Access-specifier, parameters and return statement are optional.

Let's see an example in which we have created a function that returns a string value and takes a string parameter.

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

C# Function: using parameter but no return type

using System;
namespace FunctionExample
{
    class Program
    {
        // 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

A function can have zero or any number of parameters to get data. In the following example, a function is created without parameters. A function without parameter is also known as non-parameterized function.

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

Output:

Inside Show Function
Hello Rahul Kumar
Next TopicCall By Value




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf