×

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

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# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

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

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

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

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

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

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