-1

I am from java backgroud and just started studying scala .

I have few doubts.

1. What does scala use functions or methods?

Here it is given that methods are associated with object and functions are not. So java have methods and not functions.

If scala follows the concept of oops then why its called functional language explain it with some code??

2. Difference between functional and non functional language

As i started studying scala i came across "scala is a functional language".Here i get the explanation that functional programming are those which follows first class function means "Functional programming involves writing code that does not change state.". What is the advantage of the code that we cant change?(correct me if i am wrong) .what is the code difference?

It would be very helpful if someone explain me this.

Thanks in advance

Community
  • 1
  • 1
singhakash
  • 7,891
  • 6
  • 31
  • 65
  • 1
    method = code associate with an object which can change state and possibly it's arguments, function = code without an associated object, which does not change state. – Peter Lawrey Aug 03 '14 at 17:09
  • "What does that suppose to mean?" it's pretty clear, what is your doubt. Perhaps you imagine it is something more than it really is. It just means not changing state in your functions. – Peter Lawrey Aug 03 '14 at 17:10
  • 1
    The advantages of functional programming is that you have less side effects and the code is easier to reason about. This make multi-threading and maintainability easier. The downside is that it tends to be slower because not all real world problems suit functional programming. – Peter Lawrey Aug 03 '14 at 17:14
  • Voted to close because of "too broad" because asking for a general explanation of functional programming is not a specific question that fits well to StackOverflow. There is already plenty of literature about the general topic of FP. – Madoc Aug 03 '14 at 18:27
  • 1
    @Madoc edited my question,is it fits to stackoverflow now? – singhakash Aug 04 '14 at 14:37
  • @Madoc,Mureinik ,acjay ,AndrewBarber,why you all are not replying now.Is my question(after editing) fits the stackoverflow? if yes then remove the hold and if no please explaine why?? – singhakash Aug 05 '14 at 13:45
  • @singhakash StackOverflow is neither Wikipedia nor a discussion forum. Your first question about how a programming language can implement both the functional and the object oriented paradigm at the same time can easily be looked up elsewhere. Your second question is in part a misunderstanding about stateless programming, and in part a question for opinions - there is no objective advantage, so this is necessarily opinionated. Don't use SO as a discussion forum, only use it for very well-defined, non-opinionated questions that are not of a very general scope. Your questions are just too broad. – Madoc Aug 06 '14 at 01:52

2 Answers2

4

In super super SUPER simple terms:

Function: A block of code with a name that you can call any time, like example(); These are also sometimes known as "Subroutines"

Method: A function defined in a class, like myInstance.example();

State: Storing temporary values

Mutable State: values that can change:

var a: Int = 0
a = 5 // legal

Immutable State: values that cannot change:

val a: Int = 0
a = 5 // error

First Class Function: A function that takes another function as parameter, aka Higher Order Function:

def apply(f: Int => Boolean, a: Int): Int = f(a)
def even(a: Int): Boolean = a % 2 == 0

apply(even, 2) // returns true

First Class Function (continued): A function that returns another function, aka Higher Order Function:

def odd(a: Int): Boolean = a % 2 != 0
def something(a: String): Int => Boolean = if(a == "odd") odd else even

val calcOdd = something("odd")
calcOdd(3) // returns true

First Class Function (continued): A function without a name, aka Anonymous Function:

((a: Int) => a * 2)(2) // returns 4

A thing to note, something that is very important to understand, is that Subroutines don't always return something but functions in Functional Programming always do.

In Java for example, you have the type void which just means a method doesn't return anything, but in Scala (and most other FP languages) you have the type Unit instead, which means it returns noting.

Although these things sound similar, they're in fact very different concepts

Java Void:

void printSomething() { // doesn't return a value
    System.out.println("Something"); 
} 

Scala Unit:

def printSomething(): Unit = println("Something") // returns ()
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
  • I think that when putting forward definitions, you should be careful with correctness. *First-class functions* are functions reified as values (i.e. which can be stored as variables and passed to other functions). Functions that take or return other functions are *higher-order functions*. The former are a prerequisite for the latter. Also, in Scala, there are important distinctions between functions (which are values) and methods (which are not, but can be converted to function values). – acjay Aug 04 '14 at 01:12
  • (cont'd) In other languages, where methods are values, you can do interesting things like iterate over the methods in an object without explicitly using reflection. – acjay Aug 04 '14 at 01:13
  • This is wrong. In Scala, functions do *not* have names. Methods are also a kind of subroutines, but they are *not* functions. Functions are objects, they are instances of a class (an anomyous subclass of one of the `FunctionN` traits), meaning they can be stored in fields, returned from methods, passed to methods or constructors. Methods aren't objects, so you cannot do any of that. – Jörg W Mittag Aug 04 '14 at 02:33
  • @JörgWMittag I wasn't talking about Scala, I was giving a more general example, due to scala's everything-is-an-object nature, the language is constructed a little differently from other languages. That said, subroutines and functions ARE generally speaking more or less the same thing, in that they're a block of code you can call again and again, with the obvious distinction that subroutines aren't values, while functions are. – Electric Coffee Aug 04 '14 at 09:27
2

An object is an instance of a class. A method is an action which can be done by either a class or an object. A function is not a method, which means that it is not done by some object.

A class can have static members, which define its state. An object can have members which define its state. A method of either a class or an object can change the value of one or more members of the class/object, thus modifies their state. A function is not owned by a class or an object, therefore it cannot change the state of its (nonexistent object/class).

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175