- The third lesson <<The fourth lesson The second lesson>>
In this lesson you will complete the third type of functions in C # programming language:
3 - functions that do not pass it parameters, but returns a value:
And the meaning that the function does not pass, "or do not receive" parameter, that when you create the function, the square will be empty (), means that there is no value of the function want to receive.
And the meaning that the function return value, that is, when we create the function we will use keyword, which is return, and be followed by the value they bring back the function. Do not forget that you should write in the "Method header" type of value that will return the function in the "return _value _type" box.
Let's create a function and see, first let us agree what the mission of this function ... ... ... for example we want our function to print the following sentence:
"third method was called "
The process very easy, first write the function:
static string thirdMethod()
{
return ("thirdMethod was called !!");
}
And the function name is "thirdMethod"
We used the word "return" followed by the value that the function will return.
Of course, if we want to implement the function from any point in the program, only we recall as follows:
thirdMethod ();
However, the previous function "ThirdMethod", return value, then we must, when recall it, to store the value that return of them in a variable and this variable has the same type of return _value _type in the same function, then we will know any variable has type of string to store the result of the function "ThirdMethod "to become a true function call as follows:
string result = thirdMethod();
Then when you print the value of result, the function will print the result "ThirdMethod".
The full program:
class Program
{
static string thirdMethod()
{
return ("thirdMethod was called !!");
}
static void Main(string[] args)
{
string result = thirdMethod();
Console.WriteLine(result);
}
}
The fourth and last type of of function in the next lesson
0 comments:
Post a Comment