×

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# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute 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# 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# Keywords

Keywords are the reserved words with special meaning. They can’t be used as variables and Identifiers. If you want to use them as identifiers, you must use ‘@’ character prior...

1 minute read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute 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# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 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# 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# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

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# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute 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# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 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.