# Funciones

``````

In [1]:

function f(x,y)
x + y
end

``````
``````

Out[1]:

f (generic function with 1 method)

``````
``````

In [2]:

f(1, 2)

``````
``````

Out[2]:

3

``````

Por defecto la función devuelve el último valor calculado

``````

In [3]:

function g(x,y)
2x
2y
end

``````
``````

Out[3]:

g (generic function with 1 method)

``````
``````

In [4]:

g(2,1)

``````
``````

Out[4]:

2

``````

Para evitar este comportamiento en funciones más complejas se usa la palabra clave return

``````

In [5]:

function h(x,y)
return x*y
x + y
end

``````
``````

Out[5]:

h (generic function with 1 method)

``````
``````

In [6]:

h(3,5)

``````
``````

Out[6]:

15

``````
``````

In [7]:

function hipotenusa(x,y)
x = abs(x)
y = abs(y)
if (x == 0) || (y == 0)
return 0
else
return sqrt(x^2 + y^2)
end
end

``````
``````

Out[7]:

hipotenusa (generic function with 1 method)

``````
``````

In [8]:

hipotenusa(1,-2)

``````
``````

Out[8]:

2.23606797749979

``````
``````

In [9]:

hipotenusa(2,0)

``````
``````

Out[9]:

0

``````
``````

In [10]:

hipotenusa(0,0)

``````
``````

Out[10]:

0

``````
``````

In [11]:

``````