Q:

Scala program to create a currying function to add two numbers

belongs to collection: Scala User-defined Functions Programs

0

Here, we will define a currying function to add two specified numbers and return the result to the calling function.

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 create a currying function to add two numbers is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to create a currying function
// to add two numbers

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

    // Function calling
    result = AddNum(10, 20);

    printf("Result : %d\n", result);
  }
  
  // Function definition of currying function.
  def AddNum(num1: Int, num2: Int) = num1 + num2;
}

Output:

Result : 30

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.

In this program, we defined a currying function AddNum() with two integer arguments. The AddNum() function adds two specified numbers and returns the result to the calling function.

In the main() function, we created an integer variable result, initialized with 0. Then we called the AddNum() function with values 10, 20. The AddNum() function returns the addition of arguments, which is assigned to the result variable. After that, we printed the result 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 create an anonymous function with... >>
<< Scala program to pass an array into user-defined f...