Lesson 10: Learning to work with Function functions in C #

Today this our lesson no 10 and today we will Learning to work with Function functions in C #.

A function in C # helps to place a series of commands into a specified container Data and then call them using the function name wherever you require the programme.

When working on a project, you may need to use a piece of code in multiple locations; this is when functions come in helpful.

You can partition your project code into different, separable portions with functions, on the other hand.

In C #, a function is generally defined as follows:

()
{

}

Simply write a function’s name followed by an open and closed parenthesis in front of it to call it.

You put the list of parameters in parenthesis if your function has one or more parameters. The following is an example:

FunctionName (Parameter1, Parameter2,…);

In the following example code, we call a function called Dostuff:

public void DoStuff ()
{
Console.WriteLine (“I’m doing something …”);
}

Learning to work with Function functions in C #

The first portion of the function call, the word public, determines the function field of view at the application level in the above function code. And deciding on it is entirely up to you.

A function’s field of view defines whether or not other functions and classes exist in its context. Whether or not the programme can view and access the targeted function is debatable.

Other classes and functions in the programme can access the above function if it is marked as public.

Additional Note: If the field of view for a function is not given, it is assumed to be Private by default.

Members of the desired function’s private class have immediate access to the function. The field of vision, also known as the scope of functions, will be thoroughly examined in the classes that follow.

The data type of the function’s output value is specified in the following phase of the function call. In C # or Void, this value can be set to any specific data type.

Members of the desired function’s private class have immediate access to the function. The field of vision, also known as the scope of functions, will be thoroughly examined in the classes that follow.

The data type of the function’s output value is specified in the following phase of the function call. In C # or Void, this value can be set to any specific data type.

Let’s tweak the above function a little bit:

public int AddNumbers(int number1, int number2)
{
int result = number1 + number2;
return result;
}

Learning to work with Function functions in C #

We modified practically every area in the new code. Instead of printing text on the output, the function now produces an integer numeric value as output, receives an integer in the numeric parameter, and conducts a mathematical operation and returns its outcome as the response variables.

That implies that we could use the aforementioned function from anywhere in the programme code and give the two integers to it to get the total of them as an output, until we required to rebuild the mathematics processes code.

The Add Number () method can be invoked from anywhere in the programme using the following code, helps to save time and programming time.

int result = AddNumbers (10, 5);
Console.WriteLine (result);
33

This function, as previously said, returns a numeric value. When we use a data type other than Void in a function, we’ve forced the function to return something.

Noticed: Whenever you remove the returning code line from of the recommended method above, the compiler will exit while the programme is running.

This function gives an error and issues the following message:

‘AddNumbers(int, int)’: not all code paths return a value

The preceding message reminds us that, although having an output value, the aforementioned method does not return anything. However, if we wish to control the function’s output, we can use the following code.

public int AddNumbers(int number1, int number2)
{

int result = number1 + number2;
if (result > 10)
{
return result;
}
}

However, as in the previous case, an error notice is still displayed. Why is that? This is because there is no guarantee that the if statement’s condition will be met and the programme will produce a result (return statement will be executed).

To solve the problem, set a default value for the return statement like this:

public int AddNumbers(int number1, int number2)
{

int result = number1 + number2;
if (result > 10)
{
return result;
}
return 0;
}

The extra code solves our program’s problem while also demonstrating that we can have several return statements inside the body of our function.

The output of the function is transmitted and the execution of the rest of the function commands is cancelled as soon as the return command is executed anywhere in the programme code.

If indeed the returned output variable’s value is more than 11, the command; 0 return is never executed in the example above.

%d bloggers like this: