C# 함수

PHPz
풀어 주다: 2024-09-03 15:13:53
원래의
983명이 탐색했습니다.

C# 함수는 함수의 참조로 사용되는 함수 이름, 함수에서 연산되는 데이터의 반환 유형, 함수의 논리적 본문, 함수에 대한 인수로 전달될 수 있는 매개변수와 프로그램 내에서 함수의 접근성을 정의하기 위한 액세스 지정자입니다. C# 프로그램에 통합할 수 있는 다양한 함수는 매개변수가 있거나 없는 함수의 조합으로, 제공된 요구 사항에 따라 반환 값을 가질 수도 있고 가질 수 없습니다.

함수에는 여러 구성요소가 있습니다 –

  • 함수 호출을 하기 위해 Function name이라는 고유한 이름이 있습니다.
  • 반환 유형을 사용하여 반환 값 데이터 유형을 지정합니다.
  • 실행 가능한 명령문을 포함하는 명령문 블록을 Body라고 합니다.
  • 함수 호출 중에 매개변수 인수 목록으로 함수를 전달할 수 있습니다.
  • 애플리케이션 기능의 접근성을 지정하기 위해 액세스 지정자를 사용할 수 있습니다.

다른 C# 함수

  • 매개변수(인수)와 반환형이 없습니다.
  • 매개변수(인수)는 있지만 반환 유형은 없습니다.
  • 매개변수(인수)를 사용하고 반환 유형을 사용합니다.
  • 매개변수(인수)가 없고 반환값이 있습니다.

C# 함수 구문

<access-specifier><return-type>FunctionName(<parameters>)
{
// function body
// return statement
}
로그인 후 복사

반환문, 매개변수, 액세스 지정자는 위 구문에서 선택사항입니다.

 Functional Aspects  Syntax(Function)
With parameters and with
return values
Declaration: int display ( int );

Function call: display ( value );

Function definition:
int display(int value)
{
statements;
return value;
}

With parameters and without
return values
Declaration: void display ( int );

Call: display (value);

Function definition:
void display ( int value)
{
statements;
}

 Without parameters and without
return values
Declaration: void display ();

Call: display ();

Definition:
void display ()
{
statements;
}

Without parameters and with
return values
Declaration: int display ( );

Call: display ( );

Definition:
int display ( )
{
statements;
return value;
}

 기능적 측면  구문(함수) 매개변수 사용 및 사용
반환값 선언: int 디스플레이( int ); 함수 호출: 표시(값); 기능 정의:
int 표시(int 값)
{
진술;
반환값;
} 매개변수 포함 및 제외
반환값 선언: void 표시( int ); 통화: 디스플레이(값); 기능 정의:
무효 표시( int 값)
{
진술;
}  매개변수가 없는 경우와 없는 경우
반환값 선언: 표시 무효(); 통화: 디스플레이(); 정의:
무효 표시()
{
진술;
} 매개변수 없이 및 사용
반환값 선언: int 디스플레이( ); 통화: 디스플레이( ); 정의:
정수 표시( )
{
진술;
반환값;
}

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.

1. Using Without Parameters and Without Return Type

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:

C# 함수

2. Using With Parameters (Arguments) and Without Return Type

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:

C# 함수

3. Using With Parameters (Arguments) and with Return Type

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:

C# 함수

4. Using Without Parameters (Arguments) and with Return Value

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:

C# 함수

C# Passing Parameters to Methods

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.

Conclusion – C# Functions

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#.

위 내용은 C# 함수의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!