C# Function

tutorial and example
tutorial and example

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.


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


inside display function

C# function Example: passing parameters into the function


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.


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.