×

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations:
  1. Function declaration
  2. Function definition
  3. Function calling
Declaring function is defining the signature of the function i.e. its return type, parameters, access modifiers. Let's see the syntax of function declaration and definition.
Syntax:
<access-modifier><return-type><function-name> (<parameter 1>,<parameter 2>…..)
{
//body statement1
//statement 2
//statement 3
//statement n
//return statement
}
Writing access modifier and parameters is optional which means a function may or may not accept any parameters. The declaration of any function includes following components:
  1. Function name: it is a unique name which represents the function. This name is called when you call the function.
  2. Return Type: this is the type which the function returns. It may or may not be void.
  3. List of parameters: this is the list of parameters which function accepts. A function may or may not accept any parameter.
  4. Access modifier: this defines the level of accessibility of the function in the application
Function Definition includes the body of the function which contains the series of executable statements. When you make a function call by using appropriate signature, the body of the function gets execution and may or may not return any value depends upon function declaration. Let's see a simple example of a function which neither accept any parameter nor return any value.

C# Function Example: without parameter and without return type
using System;
public class FunctionExample1
{
            void display()
            {
                        Console.WriteLine("inside display function");
            }
            public static void Main (string[] args)
            {
                        FunctionExample f=new FunctionExample();
                        f.display();
            }
}
Output inside display function

C# function Example: passing parameters into the function
using System;
public class FunctionExample2
{
void display(int num)
{
for(int i=1;i<=10;i++)
{
Console.WriteLine(num+ " X  "+i+ " = "+num*i);
}
}
public static void Main (string[] args)
{
FunctionExample2 f=new FunctionExample2();
f.display(5);
}
}
Output
5 X  1 = 5
5 X  2 = 10
5 X  3 = 15
5 X  4 = 20
5 X  5 = 25
5 X  6 = 30
5 X  7 = 35
5 X  8 = 40
5 X  9 = 45
5 X  10 = 50
A function can accept any number of parameters. The function which doesn’t accept any parameter is called non-parameterized function.

C# Function Example: returning value from a function and passing parameters.
using System;
public class function_example3
{
            public string display (string message)
            {
                        message+=" and this is returned from display function";
                        return message;
            }
            public static void Main(string[] args)
            {
                        function_example3 p1=new function_example3();
                        string m=p1.display("this is passed from main");
                        Console.WriteLine(m);
                      
            }
}
Output this is passed from main and this is returned from display function Actual and formal parameters: When you call the function then the variables passed in the function call are called actual arguments or actual parameters. In the function definition, each actual parameter is assigned to a corresponding formal parameter that are mentioned in the function signature.

Related Topics

C# First Application

Let's see the first simple program in C# which prints Hello word on the console. class FirstExample { static void Main (string[] args) { System.Console.WriteLine("Hello World "); } } Output Hello World Description of the first Program: class: it’s...

2 minutes read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Function

A function represents a block of code that has its specific signature. Operating functions includes three operations: Function declaration Function definition Function calling Declaring function is defining the signature of the...

4 minutes read.

C# Call by reference

In C#, we can use ref keyword to pass the reference to the original value instead of the copy of the original value. the changes that are made in the...

2 minutes read.

C# Base

C# provides a base keyword to access base class properties in the chilled class. C# Base is almost similar to the super in java. It can only be used inside...

3 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Aggregation

In C#, Aggregation Represents Has-A Relationship between two objects. A field of the class defines another class to reuse this in the form of association. The class is used as...

3 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

C# Member overloading

If we define two members in a class with the same name then it's called member overloading. Advantage of member overloading is the thing that we don't need to give...

4 minutes read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# this Keyword

In C#, this is a keyword used to refer current class instance. It is mainly used to distinguish instance variables of the class and formal parameters defined in a constructor...

7 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# for Loop

For loop is used to iterate a part of the code multiple times.  We must use for loop instead of using while and do while if the number of iteration...

4 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.