Scala User-defined Functions Programs (With & Without Arguments)

How to create a simple function program with and without arguments in the Scala programming language? in this post, we are going to see how you can define a simple function without arguments and without a return value.


Scala program to create a simple function

object divineseo{
    def main(args: Array[String]){
        simfun();
    }
    
    def simfun(){
        println("welcome to divineseo.com")
    }
}
Scala

In 3rd line, we called a function named simfun() , and then in the 6th line, we defined the same function and the message whatever it contains inside of it. As you can see there is no argument and nothing is there to return.

Output

welcome to divineseo.com

Scala program to create a function with arguments

object divineseo{
    def main(args: Array[String]){
        num(20 , 23);
    }
    
    def num(num1: Int, num2: Int){
        var result : Int =0
        result = num1 + num2
        println("the addition of two number is :" +result)
        result = num1 - num2
        println("the substraction of two number is: " +result)
    }
}
Scala

Output

the addition of two number is :43
the substraction of two number is: -3

Leave a Comment