self.b = b
res = a + b
}
func tot(c: Int) - > Int {
return res - c
}
func result() {
println("Result is: \(tot(20))")
println("Result is: \(tot(50))")
}
}
let pri = calculations(a: 600 , b: 300 )
pri.result()
When we run the above program using playground, we get the following result:
Result is: 880
Result is: 850
Class Calculations defines two instance methods:
init() is defined to add two numbers a and b and store it in result 'res'
tot() is used to subtract the 'res' from passing 'c' value
Finally, to print the calculations methods with values for a and b is called. Instance
methods are accessed with '.' dot syntax
Local and External Parameter Names
Swift Functions describe both local and global declarations for their variables. Similarly,
Swift Methods naming conventions also resembles as that of Objective C. But the
characteristics of local and global parameter name declarations are different for functions
and methods. The first parameter in swift are referred by preposition names as 'with', 'for'
and 'by' for easy to access naming conventions.
Swift provides the flexibility in methods by declaring first parameter name as local
parameter names and the remaining parameter names to be of global parameter names.
Here 'no1' is declared by swift methods as local parameter names. 'no2' is used for global
declarations and accessed through out the program.