当前位置:网站首页>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 .
边栏推荐
- MongoDB在腾讯零售优码中的应用
- 关于 GIN 的路由树
- Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
- (pytorch advanced path 2) word embedding and position embedding
- Development status of full color LED display
- "Forget to learn again" shell process control - 38. Introduction to while loop and until loop
- 架构师之路,从「存储选型」起步
- Community article | mosn building subset optimization ideas sharing
- TDengine 连接器上线 Google Data Studio 应用商店
- 百行代码实现基于Redis的可靠延迟队列
猜你喜欢

ML笔记-matrix fundamental, Gradient Descent

加密市场进入寒冬,是“天灾”还是“人祸”?

那些没考上大学的人,后来过的怎样

KEIL仿真和vspd

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

Driving the efficient growth of the manufacturing industry, UFIDA u9 cloud is constantly improving the password behind it

模板特例化 template<>

【VTK】模型旋转平移

【题目精刷】2023禾赛-FPGA

FPGA collects DHT11 temperature and humidity
随机推荐
推进兼容适配,使能协同发展 GBase 5月适配速递
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
mysql如何将字段修改为not null
还整成这样
C# 实现插入排序
[Newman] postman generates beautiful test reports
Verilog使用inout信号的方法
What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
On the routing tree of gin
UK considers listing arm in London based on national security
架构师之路,从「存储选型」起步
阿里云中间件的开源往事
Database connection pool: implementation of connection pool function point
二分查找(整数二分)
A simple understanding of hill ordering
求求了,别被洗脑了,这才是90%中国人的生存实况
C语言学习-18-makefile文件编写例子以及如何生成、调用动态库
普通人怎么在一年内赚到100万?
迷宫问题(BFS记录路径)
【一起上水硕系列】Day Three - video