OneBite.Dev - Coding blog in a bite size

Declare A Function With Single Parameter In C#

Code snippet for how to Declare A Function With Single Parameter In C# with sample and detail explanation

C# is an object-oriented programming language that can use functions to perform various tasks. In this article, we will look at how to declare a function with a single parameter in C#.

Code snippet for declaring a function with a single parameter in C#

The following code block represents a simple function declaration in C# with a single parameter:

static void Main(string[] args)
{
    ShowMessage("Hello, World!");
}

static void ShowMessage(string message)
{
    Console.WriteLine(message);
}

Code Explanation for declaring a function with a single parameter in C#

C# functions are declared by specifying their return type, the function’s name, and any parameters it may have. Our example function is named ShowMessage and it accepts one parameter.

The first line of our code block, static void Main(string[] args), is the entry point of our C# console application. The ‘Main’ function has an array of strings as parameters by default, and can include other local functions and operations.

In our Main() method, we have called the ShowMessage() function and passed the string “Hello, World!” as the argument. This string is the single parameter that our function will use.

We then declare the ShowMessage function in our code block, specifying a single parameter string message. This function takes in a single string as an argument when called. The void keyword before ShowMessage means that our function does not return any value.

The body of our ShowMessage function is a single line: Console.WriteLine(message);. Here, we are using the Console.WriteLine() method that comes with the .NET framework to output our message parameter to the console.

In C#, you can build functions that take in as many or as few parameters as necessary, based on your program’s requirements. This simple example demonstrates the basic concept, but remember the power of functions is in how they can be used to manipulate and manage data within your programs.

c-sharp