1.

What is the difference between a function and a method?

Answer»

Function and method both terms are used when a CODE block takes a SET of parameters and computes a value or performs some action based on the INPUTS. Such a block is called a function when it is not associated with any class. On the other hand, one such block is called a method when it is a member of a class.

For example, in the code below, getSalary() is a method of the class Employee, but sortBySalaryis function.

class Employee(val id:Long, val NAME:STRING, val salary:Int) { //other members def getSalary() = salary } Val employees: List[Employee] = … // fetch employees sortBySalary(employees)


Discussion

No Comment Found