Declare A Function With Multiple Parameter In C#
Code snippet for how to Declare A Function With Multiple Parameter In C# with sample and detail explanation
Function declaration and usage are the backbone of any programming language. In C#, functions can be defined with multiple parameters, enhancing the capability and efficiency of your program. Today, we will delve into how to declare a function with multiple parameters in C#.
Code snippet
Below is a simple illustration of a function declared with multiple parameters:
public class Program
{
public static void Main(string[] args)
{
printFullName("James", "Anderson");
}
static void printFullName(string firstName, string lastName)
{
Console.WriteLine("Full name is: " + firstName + " " + lastName);
}
}
Code Explanation
The given code snippet is a simple program that uses a function with multiple parameters to print an individual’s full name.
We start by declaring a public class
named Program
, this will serve as our main program class.
Inside it, we use the public static void Main
function, which is the entry point of any C# program. This function takes string
arrays called args
as a parameter.
Inside the Main
function, we call another function named printFullName
that we wrote. This function takes two strings, “James” and “Anderson”, as parameters.
After our Main
function, we declare the function printFullName
. It’s static, meaning you don’t need an instance of this function to call it. Its return type is void
, which means it doesn’t return anything. It takes two parameters - firstName
and lastName
- both of which are strings.
Once called, the printFullName
function uses Console.WriteLine()
to output the string “Full name is: ” followed by the firstName
parameter, a space, and then the lastName
parameter.
This is an example of how you can declare and use a function in C# with multiple parameters. With this knowledge, you can now create more complex and dynamic functions. Happy coding!