The difference of Methods and Functions

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The difference of Methods and Functions相关的知识,希望对你有一定的参考价值。

Functions

Functions are self-contained chunks of code that perform a specific task. You give a function a name that identifies what it does, and this name is used to “call” the function to perform its task when needed.

Swift’s unified function syntax is flexible enough to express anything from a simple C-style function with no parameter names to a complex Objective-C-style method with local and external parameter names for each parameter. Parameters can provide default values to simplify function calls and can be passed as in-out parameters, which modify a passed variable once the function has completed its execution.

Every function in Swift has a type, consisting of the function’s parameter types and return type. You can use this type like any other type in Swift, which makes it easy to pass functions as parameters to other functions, and to return functions from functions. Functions can also be written within other functions to encapsulate useful functionality within a nested function scope.

Methods

Methods are functions that are associated with a particular type. Classes, structures, and enumerations can all define instance methods, which encapsulate specific tasks and functionality for working with an instance of a given type. Classes, structures, and enumerations can also define type methods, which are associated with the type itself. Type methods are similar to class methods in Objective-C.

The fact that structures and enumerations can define methods in Swift is a major difference from C and Objective-C. In Objective-C, classes are the only types that can define methods. In Swift, you can choose whether to define a class, structure, or enumeration, and still have the flexibility to define methods on the type you create.

一句话:函数是独立的,而方法是函数封装在类,结构或者枚举中的函数。

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Methods.html

https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html

以上是关于The difference of Methods and Functions的主要内容,如果未能解决你的问题,请参考以下文章

What is the maximum length of a URL in different browsers?

The difference of src and href

数列极差问题(the problem of sequence biggest difference)

[Banner] The number of titles and images is different

[Banner] The number of titles and images is different

转载:Character data is represented incorrectly when the code page of the client computer differs from