ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Problem Solution:

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

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.

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.