# ## bash_什么是函数以及如何在PHP,Python,JavaScript,C / C ++,C#,Bash,Java和PowerShell编程语言中创建函数?...

# ## bash

# ## bash

A function is a basic element of the programming languages. The function is derived from mathematics where complex operations are summed and aggregated together for multiple use cases. In programming, language function provides abstractions, simplicity, elegance for code which can be used multiple times with a simple call.

函数是编程语言的基本元素。 该函数是从数学派生而来的,其中针对多个用例将复杂的操作求和并汇总在一起。 在编程中,语言功能为代码提供了抽象,简单,优雅的特性,可通过一次简单调用多次使用。

函数语法 (Syntax Of The Function)

The function is provided with different programming languages with the different syntax but most of them are very similar and provides similar features and attributes. In this part, we will provide the syntax of the generic function and its attributes, parameters, etc.

该函数提供了具有不同语法的不同编程语言,但其中大多数非常相似,并提供相似的功能和属性。 在这一部分中,我们将提供通用函数的语法及其属性,参数等。

FUNCTION_TYPE FUNCTION_NAME(PARAMETER1,PARAMETER2,...){

  FUNCTION_BODY

}
  • FUNCTION_TYPE exist in some programming languages where it defines the type of the result of the function. For example, if we want to sum integers with a function the result should be an integer too.

    FUNCTION_TYPE存在于某些编程语言中,其中定义了函数结果的类型。 例如,如果我们想用一个函数对整数求和,那么结果也应该是整数。

  • FUNCTION_NAME defines the function identifier. This is used when calling functions. Functions are identified with the name of the function.

    FUNCTION_NAME定义函数标识符。 在调用函数时使用。 用功能名称标识功能。

  • PARAMETER is used to provide data to the function. Functions may accept single or multiple parameters or do not accept any parameter.

    PARAMETER用于向函数提供数据。 函数可以接受单个或多个参数,也可以不接受任何参数。

  • {,} curly braces are used to surround a function body. The function body or code we will execute with a function will be put into the curly braces.

    {}花括号用于包围功能主体。 我们将与函数一起执行的函数体或代码将放在花括号中。

  • FUNCTION_BODY contains the function code which will execute when the function is called or used. This can also return a value for the caller.

    FUNCTION_BODY包含将在调用或使用该函数时执行的函数代码。 这也可以为调用者返回一个值。

功能参数 (Function Parameters)

Functions are very useful with parameters. In most of the cases, a function will provide single or more parameters. The parameter may have some data type like integer, string, floating-point, object, pointer, structure, or complex types. Some parameter is optional were using given parameters is not a must and can be omitted while calling function. Functions can be defined multiple times with the same name where parameters are different in each definition. This is called function overloading.

函数对参数非常有用。 在大多数情况下,函数将提供单个或多个参数。 参数可以具有某些数据类型,例如整数,字符串,浮点数,对象,指针,结构或复杂类型。 在使用给定参数不是必须的情况下,某些参数是可选的,并且在调用函数时可以省略。 可以使用相同的名称多次定义函数,其中每个定义中的参数都不同。 这称为函数重载。

LEARN MORE  C Functions - Create and Run with Examples
了解更多C函数-使用示例创建和运行

调用函数(Calling A Function)

Calling a function can be some times tricky. When we call a function we are expecting something to happen. These can be different things like saving some file, read a file, run SQL query, execute some calculations, calling other functions, etc. The function is called by providing the function name like below where we will provide two parameters.

调用函数有时可能很棘手。 当我们调用一个函数时,我们期望会发生一些事情。 这些可以是不同的事情,例如保存一些文件,读取文件,运行SQL查询,执行一些计算,调用其他函数等。通过提供如下所示的函数名称来调用该函数,我们将提供两个参数。

FUNCTION_NAME(PARAMETER1,PARAMETERS);

返回值 (Return Value)

Functions can be created for different purposes where returning a value is very popular. Functions calculate some data and return to the caller. This return value can be different types like integer, float, string, etc. Generally return keyword is used to return provided data or result to the caller.

在返回值非常流行的情况下,可以出于不同目的创建函数。 函数计算一些数据并返回给调用者。 此返回值可以是不同的类型,例如整数,浮点数,字符串等。通常, return关键字用于将提供的数据或结果返回给调用方。

return RESULT

在PHP中创建和调用函数 (Create and Call Function In PHP)

PHP functions can be created with the function keyword before the function name. We will use curly braces in order to surround the function body. We will also use ; for each statement in the function body which is related to the PHP syntax. Here is the syntax of the PHP function.

可以在函数名称之前使用function关键字创建PHP函数。 我们将使用花括号来包围功能主体。 我们也将使用; 对于函数主体中与PHP语法相关的每个语句。 这是PHP函数的语法。

function FUNCTION_NAME(PARAMETER1,PARAMETER2,...){

   FUNCTION_BODY;

}

As an example, we will create a function that will sum two given integers and returns the result to the caller.

例如,我们将创建一个函数,该函数将两个给定的整数相加并将结果返回给调用方。

function sumNumbers($number1,$number2){

   $result=$number1+$number2;

   return $result;

}

and we can call the sumNumbers()function like below for different parameters.

我们可以像下面那样为不同的参数调用sumNumbers()函数。

$result=sumNumbers(2,3);

$result=sumNumbers(2,9);

$result=sumNumbers(8,3);

在Python中创建和调用函数 (Create and Call Function In Python)

Python syntax is different from C related language syntax. It is similar to Visual Basic programming language. We will use def keyword before the function name and add : to the function name line. The function body is nor surrounded by some characters where it is defined with spaces. The function body will be 1 tab indent. The result can be returned with the return keyword.

Python语法与C相关的语言语法不同。 它类似于Visual Basic编程语言。 我们将在函数名称之前使用def关键字,并在函数名称行中添加: 。 函数主体也没有被一些用空格定义的字符包围。 函数主体为1个制表符缩进。 可以使用return关键字返回结果。

def sumNumbers(number1,number2):

   result=number1+number2

   return result

We can call this function like below.

我们可以像下面这样调用此函数。

Create and Call Function In Python
Create and Call Function In Python
在Python中创建和调用函数

在JavaScript中创建和调用函数(Create and Call Function In JavaScript)

JavaScript function definition syntax is very similar to the Python programming language syntax definition. There is two different where the biggest one is the function body is surrounded with curly braces and another one is each statement is ended with ;. Here is the generic function definition syntax of the JavaScript programming language.

JavaScript函数定义语法与Python编程语言语法定义非常相似。 有两个不同之处,最大的一个是函数主体被大括号包围,另一个是每个语句以结尾; 。 这是JavaScript编程语言的通用函数定义语法。

function FUNCTION_NAME(PARAMETER1,PARAMETER2,...){

  FUNCTION_BODY;

}

In this example, we will create the function named sumNumber() with two parameters to sum.

在此示例中,我们将创建一个名为sumNumber()的函数, sumNumber()包含两个要求和的参数。

function sumNumbers(number1,number2){

   result = number1 + number2;

   return result;

}

We can call this function like below.

我们可以像下面这样调用此函数。

Create and Call Function In JavaScript
Create and Call Function In JavaScript
在JavaScript中创建和调用函数

在C / C ++中创建和调用函数(Create and Call Function In C/C++)

C and C++ programming languages are very old where a lot of different programming languages like PHP, JavaScript has inherited its syntax. C and C++ programming languages are system-level languages where a lot of details should be defined precisely. We need to define the function return value type and parameter value types during the function definition. Also, we need to provide proper values as a parameter and return value. The syntax is like below.

C和C ++编程语言非常古老,其中许多不同的编程语言(如PHP,JavaScript)都继承了其语法。 C和C ++编程语言是系统级语言,应精确定义许多细节。 在函数定义期间,我们需要定义函数返回值类型和参数值类型。 另外,我们需要提供适当的值作为参数并返回值。 语法如下。

RETURN_TYPE FUNCTION_NAME (PARAMETER1, PARAMETER2,...){

   FUNCTION_BODY;

}
int sumNumbers(int number1, int number2){

   int result = number1 + number2;

   return result;

}

We can call this function like below.

我们可以像下面这样调用此函数。

int result = sumNumbers(2,3);

在C#中创建和调用函数 (Create and Call Function In C#)

C# is Java and C oriented programming language where the very same syntax is used. We will use a C or C++ programming language function in a C# code without a problem like below.

C#是使用完全相同的语法的Java和C导向的编程语言。 我们将在C#代码中使用C或C ++编程语言函数,而不会出现以下问题。

RETURN_TYPE FUNCTION_NAME (PARAMETER1, PARAMETER2,...){

   FUNCTION_BODY;

}
int sumNumbers(int number1, int number2){

   int result = number1 + number2; 

   return result;

}

And we can call it like below.

我们可以像下面这样称呼它。

int result = sumNumbers(2,3);

用Java创建和调用函数 (Create and Call Function In Java)

Java is C and C++ as a programming language. Also, C# is very similar to Java. We can use C# or C functions in a Java programming language like below.

Java是C和C ++作为一种编程语言。 而且,C#与Java非常相似。 我们可以使用如下所示的Java编程语言来使用C#或C函数。

RETURN_TYPE FUNCTION_NAME (PARAMETER1, PARAMETER2,...){

   FUNCTION_BODY;

}
int sumNumbers(int number1, int number2){

   int result = number1 + number2; 

   return result;

}

and we can call it like below.

我们可以这样称呼它。

int result = sumNumbers(2,3);

在Bash中创建和调用函数 (Create and Call Function In Bash)

Bash is a very popular Linux shell. Bash provides more than a shell-like programming language features. Bash can be used to create scripts and functions. We can create some function in order to use in scripts or Bash interactive shell. There are two types of syntax to create a Bash function.

Bash是非常流行Linux Shell。 Bash提供了比shell一样的编程语言功能。 Bash可用于创建脚本和函数。 我们可以创建一些函数以便在脚本或Bash交互式shell中使用。 创建Bash函数的语法有两种。

FUNCTION_NAME(){

   FUNCTİON_BODY

}

Or we can use function keyword in order to create a function.

或者我们可以使用function关键字来创建一个函数。

function FUNCTION_NAME{

   FUNCTİON_BODY

}

We will create a function named sumNumbers which accept two parameters. In Bash functions, parameters are read inside the function body with $1,$2,… in an incremental manner.

我们将创建一个名为sumNumbers的函数,该函数接受两个参数。 在Bash函数中,以$1$2 ,…递增方式在函数体内读取参数。

sumNumbers () {
   
   number1=$1;

   number2=$2;

   result = $(( number1 + number2 ))

   return $result

}

在PowerShell中创建和调用函数 (Create and Call Function In PowerShell)

PowerShell is a shell used in Windows operating systems. It provides advanced programming language features. We can create a function like a Bash shell. We will use the functionkeyword before the function name and surround the function body with the curly braces {, }.

PowerShell是Windows操作系统中使用的shell。 它提供了高级编程语言功能。 我们可以创建一个类似于Bash shell的函数。 我们将在函数名称之前使用function关键字,并用花括号{}将函数主体括起来。

function sum-Numbers(){
   return $args[0]+ $args[1]
}

and we can call like below.

我们可以像下面这样打电话。

PS> sum-Numbers 1 2

PS> sum-Numbers 10 20
Create and Call Function In PowerShell
Create and Call Function In PowerShell
在PowerShell中创建和调用函数

翻译自: https://www.poftut.com/what-is-a-function-and-how-to-create-function-in-php-python-javascript-c-c-c-bash-java-powershell-programming-languages/

# ## bash

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值