scala 函数中嵌套函数_Scala函数–声明,定义,调用和嵌套函数

scala 函数中嵌套函数

A function is a set of statements combined together to perform a specific task. The code can be divided logically into separate functions where each function is assigned a specific task.

函数是组合在一起以执行特定任务的一组语句。 该代码可以在逻辑上划分为单独的功能,其中每个功能都分配有特定的任务。

The function in Scala is a complete object which can be assigned to a variable whereas a method in Scala is a part of class having name, signature, bytecode and annotations. Function name can have characters like ++,+,-,– etc.

Scala中的函数是一个完整的对象,可以将其分配给变量,而Scala中的方法是具有名称,签名,字节码和注释的类的一部分。 函数名称可以包含++,+,-,–等字符。

A function can be defined inside a function known as nested functions which is supported in Scala unlike in Java.

可以在称为嵌套函数的函数内部定义函数,与Java不同,Scala支持该函数。

Scala函数声明 (Scala Function Declarations)

The syntax for declaring a function is;

声明函数的语法是;

def functionName([arguments]) : return type

For example,

例如,

def multiply(a:Int ,b:Int): Int

Here def is the short form for define and multiply is the function name. a and b are parameters to the multiply function of Integer data type and the result returns an Integer data type.

def是define的简写形式,multiple是函数名。 a和b是Integer数据类型的乘法函数的参数,并且结果返回Integer数据类型。

功能定义 (Function Definition)

The function definition takes the following form;

函数定义采用以下形式;

def functionName ([list of parameters]) : [return type] = {
function body
return [expr]
}

def is the keyword to define a function, functionName is the name of the function, list of parameters are the variables separated by comma and return type is the datatype of the return value. function body contains the logic and return keyword can be used along with an expression in case function returns value.

def是定义函数的关键字,functionName是函数的名称,参数列表是用逗号分隔的变量,返回类型是返回值的数据类型。 函数主体包含逻辑和return关键字,可在函数返回值的情况下与表达式一起使用。

For example;

例如;

def multiply(a:Int,b:Int) : Int = {
var c : Int = 0
c = a*b
return c;
}

multiply is the name of the function with two variables a and b. We declare another variable c of integer data type, store the result of a*b in this variable and return the computed variable c.

乘法是具有两个变量a和b的函数的名称。 我们声明另一个整数数据类型的变量c,将a * b的结果存储在此变量中并返回计算出的变量c。

Run the program by typing multiply(50,20) in Scala shell and you will get output like res0: Int = 1000.

通过在Scala shell中键入multiply(50,20)来运行程序,您将获得类似于res0: Int = 1000输出。

A function which does not return anything can return Unit which is equivalent to void in Java. The functions which does not return anything are called procedures in scala.

一个不返回任何东西的函数可以返回Unit,它等于Java中的void。 不返回任何内容的函数在scala中称为过程。

def hello( ) : Unit = {
println("Hello, World!")
}

Here we are just printing hello world so the return data type is Unit.

在这里,我们只是打印问候世界,因此返回数据类型为Unit。

Run the program by typing hello and you will get output as Hello, World!.

通过键入hello运行该程序,您将获得输出Hello, World!

Let us see a complete example of a function by combining the declaration and definition and test the function by creating the object.

让我们通过结合声明和定义来查看功能的完整示例,并通过创建对象来测试功能。

object mult {

def main(args:Array[String]) {
println("Multiplication result of two numbers is : "+multiply(20,21));
}

def multiply( a:Int, b:Int ) : Int = {
var c:Int = 0
c = a * b
return c
}
}

The line “def multiply( a:Int, b:Int ) : Int” is the function declaration and “var c:Int = 0,c = a * b” constitute the function body. return c is the return value of the function. All the three declaration, definition and return type constitute a complete function.

行“ def multiply( a:Int, b:Int ) : Int ”是函数声明,“ var c:Int = 0,c = a * b ”构成函数体。 return c是函数的返回值。 这三个声明,定义和返回类型都构成一个完整的函数。

Run the above function code by typing mult.main(null) and see the result as below.

通过键入mult.main(null)运行上面的功能代码,并看到如下结果。

Multiplication result of two numbers is : 420

两个数字相乘的结果是:420

调用功能 (Invoking a Function)

The way of calling a function in Scala is;

在Scala中调用函数的方式是:

functionName( list of parameters )

For example,

例如,

multiply(4,3)

We can also create an instance and invoke the function as;

我们还可以创建一个实例并以以下形式调用该函数:

[instance.]functionName( list of parameters )

For example,

例如,

test.multiply(5,6)

Consider an example of calling a function;

考虑一个调用函数的例子;

object multest {

def main(args: Array[String]) {
println( "Result is : " + multiply(12,14) );
}

def multiply( a:Int, b:Int ) : Int = {
var c:Int = 0
c = a * b
return c
}
}

We are creating an object multest and defining a method multiply which accepts two variables a and b of integer data type and then perform the multiplication of a and b variables storing the result of the operation in variable c. We are writing a method main and calling the function multiply(12,14) along with the parameters.

我们正在创建一个对象multest,并定义一个方法乘法,该方法接受两个整数数据类型的变量a和b,然后执行a和b变量的乘法,将运算结果存储在变量c中。 我们正在编写方法main,并与参数一起调用函数multiple(12,14)。

Run the above code by typing multest.main(null) in the scala shell and see the following output;

通过在scala shell中键入multest.main(null)来运行以上代码,并查看以下输出;

Result is : 168

结果是:168

Now let us see an example how to invoke a function by creating an instance.

现在让我们看一个示例,该示例如何通过创建实例来调用函数。

class Multiplication {

def multiply(a : Int , b: Int ) :Int = {
var c : Int = 0
c = a * b
return c
}
}

Multiplication class is created and the multiply function is defined with variables a and b and return the variable c which stores the multiplication result of the two variables passed.

创建乘法类,并使用变量a和b定义乘法函数,并返回变量c,该变量存储所传递的两个变量的相乘结果。

Create an object multest and call the method multiply creating instance of Multiplication class as;

创建一个对象multest,并将方法Multiplication class的创建实例乘以调用方法;

object multest {

def main(args:Array[String]) {
var mul = new Multiplication()
println( "Result is : " +mul.multiply(15,16));
}
}

mul is the instance of the Multiplication class and the method is called as mul.multiply(15,16)

mul是Multiplication类的实例,该方法称为mul.multiply(15,16)

Run the code in the shell by typing multest.main(null) which produces the below output.

通过键入multest.main(null)在shell中运行代码,生成以下输出。

Result is : 240

结果是:240

嵌套函数 (Nested Functions)

A function defined inside another function is called Nested function. Scala differs from Java in this feature as nested functions are not supported in Java.

在另一个函数内部定义的函数称为嵌套函数。 Scala与Java的不同之处在于此功能,因为Java不支持嵌套函数。

Consider an example of nested function.

考虑一个嵌套函数的例子。

def min(x: Int, y: Int, z: Int) = {
def min(i: Int, j: Int) = if (i < j) i else j
min(x,min(y,z))
}

The method min accepts three parameters x,y and z of type Integer and then again we define min func to find the minimum of two numbers first and then with the result of the two found and the number we find the minimum. The minimum of 3 numbers function is defined first and then minimum of two numbers is defined and these are known as nested functions.

min方法接受Integer类型的三个参数x,y和z,然后再次定义min func,以首先找到两个数字的最小值,然后使用找到的两个结果和该数字找到最小值。 首先定义最少3个数字的函数,然后再定义最少2个数字,这些被称为嵌套函数。

Run the above by calling min function as min(12,34,6) and you will get result as res21: Int = 6.

通过将min函数调用为min(12,34,6)运行以上min(12,34,6) ,您将得到结果为res21: Int = 6

Nested functions are not required to be of same name, they can have different name too.

嵌套函数不必具有相同的名称,它们也可以具有不同的名称。

That’s all for functions in Scala programming language, we will look into more Scala core features in coming articles.

这就是Scala编程语言中的所有功能,我们将在后续文章中研究更多Scala核心功能。

翻译自: https://www.journaldev.com/7713/scala-functions-declaration-definition-invocation-and-nested-functions

scala 函数中嵌套函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值