Q:

Scala program to call a function using curly braces

belongs to collection: Scala User-defined Functions Programs

0

Here, we will create a user-defined function and then call that function using curly braces and print appropriate results on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to call a function using curly braces is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to call a function
// using curly braces

object Sample {
  def main(args: Array[String]) {
    var num: Int = 10;

    printNumber {
      num += 2; num
    }
  }

  def printNumber(num: Int) {
    printf("Number is: %d\n", num);
  }
}

Output:

Number is: 12

Explanation:

In the above program, we used an object-oriented approach to create the program. We created an object Sample, and we defined main() function. The main() function is the entry point for the program.

Here, we created a function printNumber() to print the value of a specified number on the console screen.

In the main() function, we called function using curly braces and update the value of the argument during the function call. Then we printed the value of the specified number on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Scala program to demonstrate the partially applied...