- To Create a function to add 2 numbers
User Input: 2,3
Output: 5
import java.util.Scanner
fun sum(n1:Int,n2:Int):Int {
return n1+n2
}
fun main(args: Array<String>) {
print("Enter two numbers : ")
val userInput = Scanner(System.`in`)
var n1:Int = userInput.nextInt()
var n2:Int = userInput.nextInt()
print("Sum : ${sum(n1,n2)}")
}
Enter two numbers : 7 5 Sum : 12Program
//function to add 2 numbers
fun sum(n1:Int, n2:Int):Int {
return n1+n2; //use add(+) operator
}
Since, function returns integer data type, so we declare Int
with :
just after the function
For devision (/), subtraction (-), multiplication (*), you can use their respective operators
ConceptComing Soon !
QuickIf your happiness depends on what somebody else does, I guess you do have a problem.