A function is a block of code designed to perform a specific task. Functions help improve code reusability and readability.
// Function Definition
void SayHello()
{
Console.WriteLine("Hello, World!");
}
// Function Call
SayHello();
A function can accept arguments to process data.
using System;
class Program
{
// Function with input parameters
static void GreetUser(string name)
{
Console.WriteLine($"Hello, {name}!");
}
static void Main()
{
Console.Write("Enter your name: ");
string userName = Console.ReadLine(); // Taking input from the user
GreetUser(userName); // Passing the input to the function
}
}
// output
Enter your name: John
Hello, John!
A function can return a value using the return statement.
using System;
class Program
{
// Function that returns the sum of two numbers
static int Add(int a, int b)
{
return a + b; // Returning the sum
}
static void Main()
{
int result = Add(5, 3); // Calling the function and storing the result
Console.WriteLine($"Sum: {result}");
}
}
// output
Sum: 8
A default parameter in C# is a function parameter that has a default value assigned to it. If the caller does not provide an argument for that parameter, the default value is used.
using System;
class Program
{
static void Greet(string name = "Guest")
{
Console.WriteLine($"Hello, {name}!");
}
static void Main()
{
Greet(); // Output: Hello, Guest!
Greet("Alice"); // Output: Hello, Alice!
}
}
You can define multiple functions with the same name but different parameter types or numbers.
void PrintValue(int value)
{
Console.WriteLine($"Integer: {value}");
}
void PrintValue(string value)
{
Console.WriteLine($"String: {value}");
}
PrintValue(10); // Output: Integer: 10
PrintValue("Overloading example"); // Output: String: Overloading example
The out keyword in C# allows a method to return multiple values by passing arguments by reference. Unlike return, it enables modifying variables declared outside the function.
using System;
class Program
{
static void Divide(int a, int b, out int quotient, out int remainder)
{
quotient = a / b;
remainder = a % b;
}
static void Main()
{
int q, r; // No need to initialize
Divide(10, 3, out q, out r);
Console.WriteLine($"Quotient: {q}, Remainder: {r}");
}
}
// output
Quotient: 3, Remainder: 1
The ref keyword in C# allows a method to pass arguments by reference rather than by value. This means that changes made to the parameter inside the function affect the original variable outside the function.
// Example 1: Using ref to Modify a Variable
using System;
class Program
{
static void DoubleValue(ref int number)
{
number *= 2; // Modify the original variable
}
static void Main()
{
int value = 5; // Must be initialized before passing
Console.WriteLine("Before: " + value);
DoubleValue(ref value); // Pass by reference
Console.WriteLine("After: " + value); // Original variable is modified
}
}
// output
Before: 5
After: 10
// Example 2: Swapping Two Numbers Using ref
void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
int x = 3, y = 7;
Console.WriteLine($"Before Swap: x = {x}, y = {y}");
Swap(ref x, ref y); // Swap using ref
Console.WriteLine($"After Swap: x = {x}, y = {y}");
// output
Before Swap: x = 3, y = 7
After Swap: x = 7, y = 3
The params keyword in C# allows a method to accept a variable number of arguments as an array. This is useful when you don’t know how many parameters will be passed.
using System;
class Program
{
static void PrintNumbers(params int[] numbers)
{
foreach (int num in numbers)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
static void Main()
{
PrintNumbers(1, 2, 3, 4, 5); // Passing multiple values
PrintNumbers(10, 20); // Works with any number of arguments
PrintNumbers(); // Even works with no arguments
}
}
A recursive function is a function that calls itself to solve a problem. It breaks down a complex problem into smaller subproblems of the same type.
using System;
class Program
{
static int Factorial(int n)
{
if (n == 1) // Base case: Stops recursion
return 1;
return n * Factorial(n - 1); // Recursive case
}
static void Main()
{
Console.WriteLine(Factorial(5)); // 5! = 5 * 4 * 3 * 2 * 1 = 120
}
}
In C#, an arrow function is a concise way to write methods or lambda expressions using the => (lambda operator). It is commonly used for short, single-expression methods or functions.
// basic function
static int Add(int a, int b)
{
return a + b;
}
// arrow function
static int AddArrow(int a, int b) => a + b;
// basic function
static void PrintMessage()
{
Console.WriteLine("Hi");
}
// arrow function
static void PrintMessageArrow() => Console.WriteLine("Hi");
static void Main(string[] args)
{
// C# Arrow Function (=>)
Console.WriteLine(Add(3, 5)); // output: 8
Console.WriteLine(AddArrow(3, 5)); // output: 8
PrintMessage(); // output: Hi
PrintMessageArrow(); // output: Hi
}