函數要先聲明才能調用,scala 函數中嵌套函數_Scala函數–聲明,定義,調用和嵌套函數

 2023-11-19 阅读 27 评论 0

摘要: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. 函數是組合在一起以執行特定

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 函數中嵌套函數

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/183133.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息