Comparing JavaScript's and Swift's Functions, Methods, and Subroutines
The tables below show the syntactically highlighted Functions, Methods, and Subroutines topic for the two languages.
If you see any incorrect information, please help us improve! Feel free to either create an issue or use the links at the bottom of the language columns to correct any information.
Void Functions
Concept
JavaScript's Implementation
Swift's Implementation
Function that does not return a value and takes no parameters
function method() {
}
func functionName() {
statements
}
Function that does not return a value and that takes 1 or more defined parameters
function method(param1, param2) {
}
func functionName(parameterOne: Type, parameterTwo: Type) {
statements
}
Function that does not return a value and that takes 0 or more defined keyword parameters
function method({param1, param2}) {
}
Unknown
Function that does not return a value and function that takes an unknown number of parameters
function method(...params) {
}
func functionName(parameterName: Type...) {
statements
}
Values passed in are made available within the function’s body as an array of type [Type].
Return Value Functions
Concept
JavaScript's Implementation
Swift's Implementation
Function that returns a value and takes no parameters
function method() {
return true;
}
func functionName() -> ReturnType {
statements
return expression
}
Function that returns a value and takes 1 or more defined parameters
function method(param1, param2) {
return true;
}
func functionName(parameterOne: Type, parameterTwo: Type) -> ReturnType {
statements
return expression
}
Function that returns a value and takes 0 or more defined keyword parameters
function method({param1, param2}) {
return true;
}
Unknown
Function that returns a value and takes an unknown number of parameters
function method(...params) {
return true;
}
func functionName(parameterName: Type...) -> ReturnType {
statements
return expression
}
Values passed in are made available within the function’s body as an array of type [Type].
Lambda/Anonymous Functions
Concept
JavaScript's Implementation
Swift's Implementation
Anonymous function that takes no parameters
const method = function() {
}
const method = () => {
}
{ expression }
Anonymous function that takes 1 or more defined parameters
const method = function(param1, param2) {
}
const method = (param1, param2) => {
}
{ (parameterOne: Type, parameterTwo: Type) -> ReturnType in
return expression
}
// with partial type inference
{ (n1: Int, n2) in n1 + n2 }
// full type inference
["A", "B", "C"].sorted(by: { s1, s2 in s1 > s2 })
Depending on what Swift can infer from the context, parameter types and return type may be optional.
Anonymous function that takes 0 or more defined keyword parameters
const method = function({param1, param2}) {
}
const method = ({param1, param2}) => {
}
Unknown
Anonymous function that takes an unknown number of parameters
const method = function(...params) {
}
const method = (...params) => {
}
{ (parameter: Type...) -> ReturnType in
return expression
}
Depending on what Swift can infer from the context, return type may be optional.
Lambda/Anonymous Functions with Return Value
Concept
JavaScript's Implementation
Swift's Implementation
Anonymous function that takes no parameters and returns a value
const method = function() {
return true;
}
const method = () => true
Unknown
Anonymous function that takes 1 or more defined parameters and returns a value
const method = function(param1, param2) {
return true;
}
const method = (param1, param2) => true
Unknown
Anonymous function that takes 0 or more defined keyword parameters and returns a value
const method = function({param1, param2}) {
return true;
}
const method = ({param1, param2}) => true
Unknown
Anonymous function that takes an unknown number of parameters and returns a value
const method = function(...params) {
return true;
}
const method = (...params) => true
Unknown
Subroutines
Concept
JavaScript's Implementation
Swift's Implementation
Call subroutine
Not Implemented In This Language
// No parameters
foo()
// With named parameters
foo(parameterName: argument)
Calls function foo.
If function definition contains named parameters, must include parameter names in function call.
If function definition contains named parameters, must include parameter names in function call.
Return from subroutine
Not Implemented In This Language
return
If entire function body is a single expression, no return keyword needed to return expression.