Home »
Scala »
Scala Programs
Scala program to create a currying function to add two numbers
Here, we are going to learn how to create a currying function to add two numbers in Scala programming language?
Submitted by Nidhi, on May 27, 2021 [Last updated : March 09, 2023]
Scala – Creating a Currying Function
Here, we will define a currying function to add two specified numbers and return the result to the calling function.
Scala code to create a currying function to add two numbers
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]): Unit = {
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.
Scala User-defined Functions Programs »