C# Function

Functions declaration process in C#.

* We already discussed the concept of built-in & user-defined functions in the C language function chapter.

What is Function?

A function is a block of code that performs a specific task. Functions are also known as methods in C#. They are defined within classes and can be called to execute their functionality. Here's how you define a function in C#:

// Function definition
public returnType FunctionName(pmType parameter1, pmType parameter2, ...)
{
    // Function body
    // Perform some tasks
    return result; // Optionally return a value
}

Let's break down the components of a function:

1. Access Modifier: This defines the visibility of the function. public, private, protected, etc.

2. Return Type: This specifies the type of value that the function will return after execution. It can be 'void' if the function doesn't return any value.

3. Function Name: The name of the function which is used to call it.

4. Parameters: These are optional and define the variables that the function expects as input. They are enclosed in parentheses and separated by commas.

5. Function Body: This is the block of code enclosed within curly braces {}. It contains the statements that define what the function does.

6. Return Statement: If the function has a return type other than 'void', it must return a value of that type using the 'return' keyword.

To call a function in C#, you simply use its name followed by parentheses containing the arguments (if any). Here's an example:

cs Copy Code
using System;

public class Program
{
    // Function definition
    public static int Add(int a, int b)
    {
        return a + b;
    }

    public static void Main(string[] args)
    {
        int result = Add(5, 3); // Calling the function
        Console.WriteLine("Result of addition: " + result);
    }
}
Output:
Result of addition: 8

In this example, the 'Add' function is defined to take two integer parameters and return their sum. Inside the 'Main' method, we call the 'Add' function with arguments '5' and '3', and the result is stored in the 'result' variable which is then printed to the console.

Functions can also return values using the 'return' statement. This allows functions to compute a result and then pass that result back to the caller.

Function Parameters

Function parameters are variables declared in the function signature that are used to pass values into the function when it is called. They allow functions to accept input data and operate on it. Function parameters are defined within parentheses following the function name and precede the function body.

Example of a function with parameters:

// Function with two parameters
void Add(int a, int b)
{
    int sum = a + b;
    Console.WriteLine("The sum of {0} and {1} is {2}", a, b, sum);
}

In this example, 'int a' and 'int b' are the parameters of the 'Add' function. When you call this function, you would provide two integer values as arguments, which would be assigned to the variables 'a' and 'b' within the function.

You can have functions with no parameters as well as functions with multiple parameters. The types and order of parameters must match between the function declaration and function call.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.