ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Problem Solution:

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

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.

Scala User-defined Functions Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.