csharp-que-son-las-funciones

What are and how to use functions in C#

  • 6 min

An function is a reusable block of code that performs a specific task when called or invoked from another part of the program.

Functions are a fundamental part of programming, as they allow dividing code into smaller, more manageable units (this facilitates understanding, maintenance, and debugging of software).

If you want to learn more, check out the Introduction to Programming Course

Functions in C# can take zero or more parameters as input, perform calculations or manipulations based on those parameters, and optionally return a result as output.

The basic syntax for defining a function in C# is as follows:

[modifier] return_type function_name([parameter_list])
{
    // Function code
    return return_value; // Optional
}
Copied!
  • Modifier: Can be public, private, protected, among others, to define the access level of the function.
  • Return type: Specifies the data type that the function will return as a result. Can be void if the function does not return any value.
  • Function name: The unique identifier of the function used to invoke it from other parts of the program.
  • Parameter list: Variables used to pass information to the function when it is called.
  • Function code: The set of instructions that will be executed when the function is called.
  • Return value: The result returned by the function, optionally.

Basic Example

Below is a basic example of a function that adds two numbers:

public int Add(int num1, int num2)
{
    return num1 + num2;
}
Copied!

In this example, the Sumar function takes two parameters num1 and num2, performs the addition operation, and returns the result.

Calling Functions

To call (invoke) a function and execute its code, you simply use the function name followed by parentheses, passing the parameter values if necessary.

Here is an example of code that calls the Sumar function and stores the result in a variable:

int result = Add(5, 3);
Copied!

In this case, the Sumar function is called with the values 5 and 3 as arguments, and returns the result 8, which is assigned to the variable resultado.

Parameters

Functions in C# can have one or more input parameters, which are values passed to the function when it is called. These parameters are used inside the function to perform calculations and determine the result to return.

For example, in the following example the CalcularAreaRectangulo function has two parameters, ancho and altura, both of type double.

double CalculateRectangleArea(double width, double height)
{
    // Code to calculate the area of a rectangle
    double area = width * height;
    return area;
}
Copied!

Return Value

Functions can return a value as a result using the return keyword. The type of the returned value must match the return type specified in the function’s signature.

For example, the CalcularAreaCirculo function returns a double type.

static double CalculateCircleArea(double radius)
{
    // Code to calculate the area of a circle
    double area = Math.PI * radius * radius;
    return area;  // return of area
}
Copied!

Only one value can be returned from a function. However, it is possible to return a grouping of several values (like a tuple, struct, or an object).

Functions Without Return

If a function does not return any value, the void keyword is used as the return type.

public void Greet(string name)
{
    Console.WriteLine($"Hello, {name}!");
}
Copied!

Practical Examples

These examples have the purpose of showing how to use Functions. It does not mean it is the best way to solve the problem they address.