Fungsi C# ialah bahagian penting program C# yang boleh terdiri daripada beberapa elemen, seperti nama fungsi yang digunakan sebagai rujukan fungsi, jenis pengembalian data yang dikendalikan dalam fungsi, badan logik fungsi, parameter yang boleh dihantar sebagai hujah untuk fungsi, dan Penentu Akses untuk menentukan kebolehcapaian fungsi dalam program. Fungsi berbeza yang boleh disepadukan ke dalam program C# ialah gabungan fungsi dengan atau tanpa parameter, yang boleh atau tidak mempunyai nilai pulangan, bergantung pada keperluan yang disediakan.
Terdapat beberapa komponen dalam fungsi –
Sintaks Fungsi C#
<access-specifier><return-type>FunctionName(<parameters>) { // function body // return statement }
Pernyataan pengembalian, parameter dan penentu Akses adalah pilihan dalam sintaks di atas.
|
Sintaks(Fungsi) | ||||||||||
Dengan parameter dan dengan pulangkan nilai |
Pengisytiharan: int paparan ( int );
Panggilan fungsi: paparan ( nilai );
Takrifan fungsi: paparan int(nilai int) { kenyataan; nilai pulangan; } |
||||||||||
Dengan parameter dan tanpa pulangkan nilai |
Pengisytiharan: paparan batal ( int );
Panggilan: paparan (nilai);
Takrifan fungsi: paparan kosong ( nilai int) { kenyataan; } |
||||||||||
Tanpa parameter dan tanpa pulangkan nilai |
Pengisytiharan: paparan batal ();
Panggilan: paparan ();
Takrif: paparan kosong () { kenyataan; } |
||||||||||
Tanpa parameter dan dengan pulangkan nilai |
Pengisytiharan: int paparan ( );
Panggilan: paparan ( );
Takrif: paparan int ( ) { kenyataan; nilai pulangan; } |
If a function’s return value is “void,” it cannot return any values to the calling function.
Note: If the return value of the function, such as “int, double, float, string, etc.” is other than void, then it can return values to the calling function.We specified the function with no parameter and no return type, a function that does not return any values here, as void type as a return type value. In this program, any values should not be passed to the function call Display(), and also, there are no values that are returned from this function call to the main function.
Let’s see the example with a function build without a return type and parameter,
Example:
Code:
using System; namespace FunctionSamples { class Program_A { // User defined function without return type and parameter public void Display() { Console.WriteLine("Non Parameterized Function"); // No return statement } static void Main(string[] args) // Main Program { Program_A program = new Program_A (); // to create a new Object program.Display(); // Call the Function } } }
Output:
In this program, a string is passed as a parameter to the function. This function’s return type is “void,” and no values can be returned. The value of the string is manipulated and displayed inside the function itself.
Example:
Code:
using System; namespace FunctionSample { class Program_B { public void Display(string value) // User defined function without return type { Console.WriteLine("Hello " + value); // No return statement } static void Main(string[] args) // Main function { Program_B program = new Program_B(); // Creating Objec program.Display("Welcome to C# Functions"); // Calling Function } } }
Output:
In this program, a string is passed as a parameter to the function. The return type of this function is “string,” and the return value of the string can be returned from the function. The value of the string is manipulated and displayed inside the function itself.
Example
Code:
using System; namespace FunctionsSample { class Program_C { // User defined function public string Show(string message) { Console.WriteLine("Inside the Show Function Call"); return message; } // Main function static void Main(string[] args) { Program_C program = new Program_C(); string message = program.Show("C# Functions"); Console.WriteLine("Hello "+message); } } }
Output:
In this program, arguments or parameters will not be passed to the function “calculate” but to the main function; the values are returned from this calculate () function call. The variables a and b values are calculated in the function called “calculate,” and in the main function, the sum of these values is returned as a result.
Example:
Code:
using System; namespace FunctionsSample { class Program_D { public void calculate() { int a = 50, b = 80, sum; sum = a + b; Console.WriteLine("Calculating the given to values: " +sum); } static void Main(string[] args) // Main function { Program_D addition =new Program_D(); addition.calculate(); } } }
Output:
When creating a method with arguments/parameters in c#, we must pass arguments/parameters to that specified method when calling our application’s function. We have several ways to pass parameters to the method; let’s see the parameters/arguments.
Parameters | Description |
Value Parameters | Value parameters are called the “input parameters.” Instead of the original parameters, the input parameters will pass a copy of the actual value; due to that, there will not be any cause or changes made to the parameter during the called method, and it will not affect on original values while the control passes to the caller function. |
Reference Parameters | Reference parameters are called the “input/output parameters.” The reference parameter will pass the reference memory of the original parameters. Thus, the changes/alteration made to the parameters in called method, while the control returns to the caller function, affects the actual values. |
Output Parameters |
It is an “output parameter” like the reference type parameters. The only difference is there is no need to initialize it before passing the data. |
In this article, we well-read the usage of the functions/ methods available in C# and learned the different C# functions. I hope this article has helped you understand the several functional aspects of C#.
Atas ialah kandungan terperinci Fungsi C#. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!