Scope/Function names and labels: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 290:
</pre>
 
=={{header|haskell}}==
Functions are considered global in haskell and can be referenced in the sections of code appearing before the function definition. The following code illustrates the same. add2 was declared after add3 and used in add3. The variables x,y and z are local to the function.
 
<lang haskell>
add3 :: Int -> Int-> Int-> Int
add3 x y z = add2 x y + z
 
add2 :: Int -> Int -> Int
add2 x y = x + y
 
main :: putStrLn(show (add3 5 6 5))
</lang>
{{Output}}
<pre>
ghci>main
16
</pre>
 
In the function below the functions g and h are local to the function getSquaredSum. They cannot be called from anywhere outside the function.
 
<lang haskell>
getSquaredSum :: Int-> Int-> Int
getSquaredSum x y = g x + h y
where
g a = a*a
h b = b*b
</lang>
{{Output}}
<pre>
ghci> getSquaredSum 3 4
25
ghci>h 4
<interactive>: 115:1:error:
Variable not in scope: h :: Integer -> t
</pre>
=={{header|Icon}} and {{header|Unicon}}==
 
Anonymous user