Skip to content

Latest commit

 

History

History

section5-function

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Functions 1 ~ 7

1) What is function? What is the difference between function and Method?

Answer:

  • Function / Method is a block of statements which performs some specific task.
  • Function is global whereas Method is associated with a Class and Structure.
// Global Scope
// Function Example
func function() {
	print("I'm a Function")
}

function()

// Method
class Car {
	func method() {
		print("I'm a Method beloing to `Car`")
	}
}

let car: Car = Car(
car.method()

2) What is inout parameter in Swift?

  • The input arguments of a method by default are let constants. To change the value of input arguments the input keyword helps.
  • To pass the references of the arguments to a method we use inout keyword.
// Without `inout`
class Car {

	func refuel(input: Double) {
		input += 10 // Error, because `input` here is `Constant`
	}

}
// With `inout`
class Car {

	func refuel(input: inout Double) {
		input += 10 // It works!
	}

}

// parameter with `&`
let car: Car = Car()
let baseValue: Double = 20.0
car.refuel(input: &baseValue)

3) Instance method vs Type Method?

  • Methods are two types: Instance methods and Type Methods.
  • Instance methods are tied up with the instance of a class. So access instance methods using object.
  • Type methods are tied up with classes. Use ClassName. TypeMethod.
  • Use static / class to declare Type Methods
class SomeClass {

	class func typeMethod() {
		print("This is TypeMethod")
	}

	static func staticMethod() {
		print("Another type of TypeMethod")
	}

	func method() {
		print("This is method")
	}

}

let someClass: SomeClass = SomeClass()

// call method
someClass.method()

// call type method
someClass.typeMethod()
someClass.staticMethod()

4) What is the difference between class func and static func ?

Answer:

  • class function — can be overridden, but static func cannot be overridden
class MyClass {

// Instance Method
func instanceMethod() {}

// Class Type Method
class func classTypeMethod() {}

// Static Type Method
static func staticTypeMthod() {}

}

// Common
MyClass.classTypeMethod()
MyClass.staticTypeMethod()

// Difference
class SubMyClass: MyClass {

	// Good!
	override class func classTypeMethod() {}

	// Compile Error
	override static func staticTypeMthod() {}

}

#Tips

  • if you use class type method with final keyword, there is no difference using static type method.

5) What is variadic number of params method?

Answer:

  • A method which accepts dynamic number of input arguments is known as variadic params method.
class MyClass {

	func variadicMethod(numbers: Int...) {
		print(numbers)
	}

}

let myClass: MyClass = MyClass()
myClass.variadicMethod(numbers: 1)
myClass.variadicMethod(numbers: 1, 2)
myClass.variadicMethod(numbers: 1, 2, 3)

6) How to set default value to a method input arguments?

Answer:

  • func myName(firstName: String, lastName=”Shin”)
class MyClass {

	func defaultValueMethod(a: Int, b: Int = 10) {
		print("a: \(a)")
		print("b: \(b)")
	}

}

let myClass: MyClass = MyClass()
myClass.defaultValueMethod(a: 10) // a: 10, b: 10
myClass.defaultValueMethod(a: 5, b: 3) // a: 5, b: 3

7) Is Swift Function Value Type or Reference Type ?

Answer:

  • It’s Reference Type

Table Of Contents

Section 1, Data Type

Section 2, Operator

Section 3, Conditional Statement

Section 4, Enum

Section 5, functions

Section 6, struct

Section 7, initializers

Section 8, closures

Section 9, OOP

Section 10, static type vs dynamic type

Section 11, optional

Section 12, generic

Section 13, subscript

Section 14, access specifier

Section 15, higher order function

Section 16, delegate

Section 17, extension

Section 18, Memory Management

Section 19, protocols

Section 20, collections

Section 21, KVO and KVC

Section 22, Exception Handling

Section 23, Framework

Section 24, Objective-C