Link Search Menu Expand Document

Introduction to Functions in C Sharp

In this article, you will see a brief introduction to C# Functions. Functions in C# are programming blocks that contain code that you want to execute at various places in your application. With functions, instead of writing the same code at different places, you write that particular piece of code inside the function and then simply call the function inside your code where you want to execute the code. Similarly, in case you need to modify a functionality, you can simply modify it inside the function without having to modify it at multiple places inside the code. Let’s now see how you can create a function, how you can pass values to a function, and how you can return values from a function.

Table of Contents

  1. Creating a C# Function
  2. Passing Values to a C# Function
  3. Returning Values from a C# Function

Creating a C# Function

Creating a function in C# is simple. You have to define the scope of the function i.e. public, private, or protected, followed by the return type and the name of the function. A pair of parenthesis is is added after the function name. A function is also called a method in C#. The following script creates a public function called ShowMessage() inside a class MyClass. The ShowMessage() function doesn’t return anything since it has a void return type. Finally, the ShowMessage() function simply prints a strong on a console.

using System;
namespace DariaApp
{
class MyClass
{
public void ShowMessage()
{
Console.WriteLine("Hello from my side");
}
}

class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.ShowMessage();

}

}
}

Output:

C Functions

To execute a C# function, you have to call it. To do so, you simply have to write a function name followed by the pair of parenthesis.  In the above script, the ShowMessage() function is called inside the void Main(string [] args) function. The void Main(string[] args) function is the starting point of a C# console application and executes automatically when a C# console application is run. Inside the Main(string[] args) function you first create an object of the MyClass class using the new keyword. Next, you call the ShowMessage() function by appending the function call ShowMesssage() with the class object mc, using a dot operator.

Passing Values to a C# Function

You can also pass values to a function. For instance, instead of printing a literal string message all the time, you can pass the message to be displayed on the console,  in the form of a variable to a function. The function then simply prints what is passed to it in the variable. To pass a value to a function, first, you have to modify a function to add function parameters.

A function parameter is basically a placeholder for the value passed to the function. The parameter type and the type of the value passed to a function should be the same. For instance, if you want to pass one string-type value to a function, you will need to define a function parameter of string type. A function parameter is defined inside the parentheses that follow the function name.

In the following script, you add one parameter string message to the ShowMessage() function. When you call the show message function, you need to pass a string that you want to display in the function call to the ShowMessage() function. The ShowMessage() function is called twice with different string values for the string message parameter.

using System;

namespace DariaApp
{
class MyClass
{
public void ShowMessage(string message)
{
Console.WriteLine(message);
}
}

class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();

// first call to ShowMessage function
mc.ShowMessage("Hello how are you?");

// second call to ShowMessage function
mc.ShowMessage("Have a good day");
}

}
}

Output:

C Sharp Functions

Returning Values from a C# Function

Finally, you can also define functions that return values to the calling function. For instance, in the following script, you define a GetSum() function which accepts two integer type parameters a and b. The function adds the two integers and returns the sum to the calling function. To return a value from a function, you can use the “return” keyword as shown in the following script.

using System;

namespace DariaApp
{
class MyClass
{
public int GetSum(int a, int b)
{
int res = a + b;
return res;

}
}

class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();

// first call to GetSum function
int sum1 = mc.GetSum(10,20);
Console.WriteLine(sum1);

// first call to GetSum function
int sum2 = mc.GetSum(50, 30);
Console.WriteLine(sum2);
}
}
}

Output:

C Functions

Other useful articles:


Back to top

© , CsharpOnline.com — All Rights Reserved - Terms of Use - Privacy Policy