Function prototype: Difference between revisions

no edit summary
mNo edit summary
No edit summary
Line 350:
 
Otherwise, Go does have the concept of a function signature which includes parameters and return values. Go is strongly typed and functions are first class objects so function signatures are used in a variety of ways. These might be considered distinct from the concept of function prototype.
 
=={{header| haskell }}==
A function can be declared without giving it's prototype in Haskell. The haskell compiler has got type inference
whereby it can infer the return type and type of variable given to function. You can still hardcode the prototype
which specifies the datatype of variables and return type. For ex. Consider a function add which takes two
integers and returns their sum. It can be prototyped and declared as :
<lang haskell>
add :: Int -> Int -> Int
add x y = x+y
</lang>
 
Actually all functions in haskell are functions with just one arguments. Haskell will treat above function as a
function which takes an int and returns a function with type (:: (Int->Int)) . Then this function which is returned
is such that it takes an int and returns an int.
Similarly for any function add which takes 3 integers and adds them the actual prototype will be as follows:
<lang haskell>
add :: Int->(Int ->(Int->Int))
</lang>
The one that does not require arguements could just be:
<lang haskell>
printThis = putStrLn("This is being printed.")
</lang>
But haskell would rather consider the function to be of return type IO() in this case.
 
Two arguments:
<lang haskell>
add :: Int -> Int -> Int
add x y = x+y
</lang>
 
The same thing can be done using the lambda function as :
<lang haskell>
add :: Int -> Int -> Int
add = \x->\y -> x+y
</lang>
 
Two arguments with unnamed parameters:
<lang haskell>
doThis :: Int-> Int-> String
doThis _ _ = "Function with unnamed parameters"
</lang>
 
Function with var args requires creation of type class as per the requirement.
 
=={{header|J}}==
Anonymous user