Pure Function In Scala Last Updated : 09 Mar, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In any programming language, there are two types of functions: 1. PURE FUNCTIONS 2. IMPURE FUNCTIONSThere is a basic difference between the two, that is pure function doesn’t change the variable it’s passed and an impure function does. For example, there exists a function which increases the input by 10. So, there are two possibilities, the first one is it could directly return x + 10 and so x does not change whereas in another case it could go with the statement like x = x + 10 and then return x, thus changing the value of x. So, the first function is pure whereas the other is impure. Pure Function in scala A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something. They are those functions which don't read any other values except those given as input and follows its internal algorithm to produce the output. A pure function is side-effect free i.e it doesn't change the value of the variable implicitly and thus doesn't end up altering the values of the input. These Scala String methods are also pure functions: isEmptylengthsubstring A pure function can be made as shown in the following program code: Example 1: Scala // Pure function In Scala object GFG { // Driver code def main(args: Array[String]) { def square(a:Int) = { var b:Int = a * a; println("Square of the number is " + b); println("Number is " + a); } square(4); } } Output: Square of the number is 16 Number is 4 Now, we know that this function don't change the variable implicitly so we add the result of two different functions in different ways, as shown: Example 2: Scala // Scala Pure Function object GFG { // Driver code def main(args: Array[String]) { // Function 1 def add(a:Int, b:Int):Int = { var c:Int = a + b; return c; } // Function 2 def multiply(a:Int, b:Int):Int = { var c:Int = a * b; return c; } // Calculating same value but changing // the position of the functions println("Output in case 1 :" + add(1, 2) * multiply(3, 4)); println("Output in case 2 :" + multiply(4, 3) * add(2, 1)); } } Output: Output in case 1 :36 Output in case 2 :36 Now, as output of both the cases are same then it is proved that input in the pure function in scala is not changed implicitly.Below are befits Of using Pure Functions: They don't cause any implicitly changes in input.They are easier to test.They can be debug easily. Comment More infoAdvertise with us Next Article Pure Function In Scala A aditya_taparia Follow Improve Article Tags : Scala Ruby-Basics Ruby-Methods Similar Reads Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact 12 min read 3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power 13 min read Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and 9 min read What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac 13 min read Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca 7 min read CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi 6 min read Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i 6 min read Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc 15+ min read Like