2017-10-07 25 views
-3

我对Swift非常陌生(Java体验)。当变量和值已经被声明时,我很困惑如何将一个变量传递给一个函数。如何将已经声明的变量传递给Swift中的函数?

func multiply(integer1: Int, integer2: Int) -> Int { 
    let returnValue = integer1 * integer2 
    return returnValue 
} 

var intX: Int = 5 
var intY: Int = 10 

print(multiply(intX, intY)) <- ????????? 

为什么我们不能像以前声明的名称那样传递变量,就像在Java,C等中一样?由于我还在学习,请允许我的无意义。任何帮助表示赞赏!干杯!

+1

您可能想阅读[定义和调用函数](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097 -CH10-ID159) –

回答

0

要调用像func multiply(integer1: Int, integer2: Int)的方法,你必须把它想:

multiply(integer1: intX, integer2: intY) 

Swift,函数的参数通常是命名参数。在调用函数时,您还必须提供带有名称的参数。

0

实际上,你可以这样调用它,通过你的声明之类的函数:

func multiply(_ integer1: Int, _ integer2: Int) -> Int { 
    let returnValue = integer1 * integer2 
    return returnValue 
} 

注_,让你忽略的参数标签。欲了解更多信息,请阅读here

相关问题