当前位置:网站首页>Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters
Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters
2022-06-22 15:45:00 【Sunshine clover lxgzxj】
One 、 Test environment
| name | edition |
|---|---|
| operating system | win10 |
| CPU | 12th Gen Intel Core i7-12700H |
| Memory | 16G |
| JDK | 1.8.0_171 |
| Scala | 3.1.2 |
Two 、 Code
object TestParameterPassing {
def main(args: Array[String]): Unit = {
ValPmtPassing(ExampleFuc(100))
NamePmtPassing(ExampleFuc(100))
FuncPmtPassing(ExampleFuc,100)
}
def ExampleFuc(Num: Int): Int = {
println(" Call the example function once ")
Num
}
// Pass value parameter test function
def ValPmtPassing(Num: Int): Unit = {
printf(" Pass value parameter function test :%d\n",Num)
printf(" Pass value parameter function test :%d\n",Num)
println("+++++++++++++++++++++")
}
// Pass name parameter test function
def NamePmtPassing(Func: => Int): Unit = {
printf(" Passing parameter function test :%d\n",Func)
printf(" Passing parameter function test :%d\n",Func)
println("+++++++++++++++++++++")
}
// Pass function parameters to test functions
def FuncPmtPassing(Func: Int => Int, Num: Int): Unit = {
printf(" Pass function parameter function test :%d\n",Func(Num))
printf(" Pass function parameter function test :%d\n",Func(Num))
println("+++++++++++++++++++++")
}
}
3、 ... and 、 Running results
Call the example function once
Pass value parameter function test :100
Pass value parameter function test :100
+++++++++++++++++++++
Call the example function once
Passing parameter function test :100
Call the example function once
Passing parameter function test :100
+++++++++++++++++++++
Call the example function once
Pass function parameter function test :100
Call the example function once
Pass function parameter function test :100
+++++++++++++++++++++
Four 、 Personal understanding
1、 Named parameter :
The name passing parameter will bring the whole function body of the incoming function into , We are in function ValPmtPassing Call two times , So he will call it completely twice ExampleFuc, Output twice : Call the example function once .
2、 Pass value parameters :
The value passing parameter will only be called once at the beginning ExampleFuc, And then bring the return value of the incoming function into , So only output once : Call the example function once .
3、 Transfer function parameters :
The effect of the implementation is the same as that of the name passing parameter , But there is a slight difference in use , Only one incoming parameter is required for a named parameter , Just pass in the method name and the parameters required by the method , The function parameters need to be passed in multiple , In addition to the function name, there are parameters , Convenience upload name parameter is much better .
边栏推荐
猜你喜欢

天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东

keil MDK 中使用虚拟串口调试串口

Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times

TDengine 连接器上线 Google Data Studio 应用商店

2022年失业的人多吗?今年是不是特别难找工作?

NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021

Charles 乱码问题解决

Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express

时隔17年,刘亦菲再次刷屏式爆红:普通人不想被淘汰,也要懂得这件事

Devsecops: best practices for ci/cd pipeline security
随机推荐
对领域驱动设计DDD理解
类似attention nlp
GBASE现身说 “库” 北京金融科技产业联盟创新应用专委会专题培训
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
曾经,我同时兼职5份工作,只为给女友买个新款耳环......
Is SQL analysis query unavailable in the basic version?
Fast and accurate point cloud registration based on minimizing 3D NDT distance
Meet webassembly again
(pytorch进阶之路二)word embedding 和 position embedding
Keil simulation and VSPD
乱解码nlp
js中const定义变量及for-of和for-in
打新债安全性有多高
ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点
架构师之路,从「存储选型」起步
Method of using inout signal in Verilog
Verilog使用inout信号的方法
What happened to those who didn't go to college
社区文章|MOSN 构建 Subset 优化思路分享
Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down