×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala program to call a function using curly braces

Here, we are going to learn how to call a function using curly braces in Scala programming language?
Submitted by Nidhi, on May 28, 2021 [Last updated : March 09, 2023]

Scala – Call a Function Using Curly Braces

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

Scala code to call a function using curly braces

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]): Unit = {
    var num: Int = 10

    // Function calling with curly braces
    printNumber {
      num += 2
      num
    }
  }

  def printNumber(num: Int): Unit = {
    println(s"Number is: $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.

Scala User-defined Functions Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.