Funcții de bază

Introducere în Julia

James Fulton

Climate informatics researcher

Ce sunt funcțiile?

Funcții utilizate anterior:

  • println()
  • typeof()
  • string()
  • push!()
  • pop!()
  • append!()
  • length()
  • sort()
x = [2,1,3]


# Takes an array, returns integer l = length(x)
# Takes an array, returns sorted array x_sorted = sort(x)
# Takes a value, prints it to console println(l)
Introducere în Julia

De ce folosim funcții?

  • Permite concentrarea pe structura programului
  • Ignorăm detaliile irelevante ale funcționării unei funcții
Introducere în Julia

Scrierea funcțiilor personalizate

# Declare function to convert temperatures
function fahrenheit2celsius(temp)

# Function body return (temp - 32) * 5/9
end
# Use function println(fahrenheit2celsius(212))
100.0
Introducere în Julia

Scrierea funcțiilor personalizate

# Declare function to convert temperatures
function fahrenheit2celsius(temp)
    # Function body
    return (temp - 32) * 5/9
end

# Use function many times
println(fahrenheit2celsius(212))
println(fahrenheit2celsius(100))
100.0
37.77
Introducere în Julia

Funcții mai lungi

# Declare function to convert temperatures
function fahrenheit2celsius(temp)
    # Function body
    temp_sub = temp - 32
    temp_c = temp_sub * 5/9
    return temp_c
end

t = fahrenheit2celsius(212) println(t)
100.0
Introducere în Julia

Funcții mai lungi

# Declare function to convert temperatures
function fahrenheit2celsius(temp)
    # Function body
    temp_sub = temp - 32       # variable inside function not available outside
    temp_c = temp_sub * 5/9
    return temp_c
end

t = fahrenheit2celsius(212)
println(temp_sub)
ERROR: UndefVarError: temp_sub not defined
Introducere în Julia

Cuvântul cheie return

function x_or_zero(x)

if x>0 return x else return 0 end
end
println(x_or_zero(-3)) println(x_or_zero(3))
0
3
Introducere în Julia

Cuvântul cheie return

# Function with longer body
function check_if_raining(rain_amount)
    is_raining = rain_amount > 0
    if is_raining
        println("Better get your coat")
    else
        println("The sky looks clear")
    end
end


check_if_raining(0.2) # Function returns nothing - only prints
Better get your coat
Introducere în Julia

Argumente multiple

function power(x, y)
    return x^y
end

# Use function to calculate 5*5
println(power(5, 2))
25
# Use function to calculate 2*2*2*2*2
println(power(2, 5))
32
Introducere în Julia

Difuzarea funcțiilor

function fahrenheit2celsius(temp)
    return (temp - 32) * 5/9
end

temps_f = [212, 32, 100]


# Function not written to work with arrays temps_c = fahrenheit2celsius(temps_f)
ERROR: MethodError: ...
Introducere în Julia

Difuzarea funcțiilor

function fahrenheit2celsius(temp)
    return (temp - 32) * 5/9
end

temps_f = [212, 32, 100]

# Broadcast function with dot syntax
temps_c = fahrenheit2celsius.(temps_f)

println(temps_c)
[100.0, 0.0, 37.77]
Introducere în Julia

Difuzarea funcțiilor

x = ["one", 2, 3.0]

# Broadcast using typeof function
println(typeof.(x))
[String, Int64, Float64]
Introducere în Julia

Difuzarea cu argumente multiple

function power(x, y)
    return x^y
end

x_arr = [1,2,3,4,5]


# Square each element of the array
println(power.(x_arr, 2))
[1, 4, 9, 16, 25]
Introducere în Julia

Difuzarea cu argumente multiple

function power(x, y)
    return x^y
end

x_arr = [1,2,3,4,5]
y_arr = [1,2,3,4,5]

# Use function on x_arr and y_arr
println(power.(x_arr, y_arr))
[1, 4, 27, 256, 3125]
Introducere în Julia

Să exersăm!

Introducere în Julia

Preparing Video For Download...