×

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

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# 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# 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# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Method Overriding

Method overriding is the mechanism where derived class defines the same method as parent class with more functionality in the chilled class. It provides runtime polymorphism in C#. It provides...

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

The C# Tutorial provides basic and advanced knowledge of C# . This will help the beginners as well as professionals in learning just more then enough about C#, A Programming...

3 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

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

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# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes 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# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

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