Julia Quick Note [10] Function (2) Application

Function (2) Application (Example of writing)

note10


◆ Type specification
 #Argument is Int type(integer)Is specified, and the return value is Float64(Real number)Specify
 function comsumption_tax(price::Int)::Float64
     tax_rate = 1.08
     return tax_rate * price
 end

◆ Type specification(One line display)
 comsumption_tax(price::Int)::Float64 = tax_rate * price


◆ Variadic argument
■ Example sentence 1
  function summation(v...)
      total = 0
      for i in v
          total += v
      end
      return total
  end

■ Example sentence 2
  function summation2(v::Int...)
      total = 0
      for i = 1:length(v)
          total += v[i]
      end
      return total
  end


◆ Optional arguments
 #Initial value of argument is set in advance(Below example: Set the initial value of v2 to 100)
 function foptional(v1, v2=100)
     total = v1 + v2
     return total
 end


◆ Keyword argument
 #As a delimiter;(semicolon)use(See the commentary below for details)
 function car(length, width; color="white", price=10000)
     println("special car:")
     println(" lenth x width = ", length, " x ", width)
     println(" color = ", color)
     println(" price = ", price)
 end


◆ Anonymous function
■ Example sentence 1
  #Only one argument can be specified
  price = x -> x * 1.08

■ Example sentence 2
  #If there are multiple lines, enclose them in begin to end.
  nebiki = x -> begin
      price = x * 0.9
      return price * 1.08
  end

Commentary

(1) Type specification

The type is not required, but it can be. By specifying the type, you can create a more rigorous and accurate program.

You can specify the type with only the argument, or you can specify the type with only the return value. Calling a function that does not match the type will result in an error.

(2) Variadic argument

** ◆ Summary ** (1) You can change the number of arguments. (2) Only the last argument can be set. (3) It is not possible to make two or more arguments variable length. (4) Type can be specified for variable length arguments.

** ① You can change the number of arguments. ** ** In the example below, there are one, two, and three arguments. image.png Since the value set in the argument is taken out as a list (array), you can also write the loop part as follows. image.png ** ② Only the last argument can be set. ** ** The following writing is not possible.

note10


function summation(v..., x)
    ...

** ③ It is not possible to make two or more arguments variable length. ** ** ** ④ You can specify the type for variable length arguments. ** ** To specify the type for variadic arguments, set as follows.

note10


function summation(v::Int...)
    ...

(3) Optional arguments

** ◆ Summary ** (1) The initial value of the argument can be set in advance. (2) Multiple arguments can be specified as options. (3) Write optional arguments together at the end of the argument.

** (1) The initial value of the argument can be set in advance. ** ** If the argument value is omitted, this initial value will be applied. image.png ** ② Multiple arguments can be specified as an option. ** ** image.png ** ③ Write optional arguments together at the end of the argument. ** ** The following writing is not possible.

note10


function foptional(v2=10, v1)
    ...

(4) Keyword argument

** Use this when you want to set the value of a specific argument among multiple arguments. ** ** In the example below, the first two arguments are normal arguments, and the price value is set among multiple arguments. Note that **; ** (semicolon) is used to separate regular and keyword arguments. image.png

(5) Anonymous function

This function is very similar to the arrow function. ** ◆ Summary ** (1) Only one argument can be specified. (2) When defining the processing of multiple lines, enclose it in begin to end.

** ① Only one argument can be specified. ** ** You cannot set multiple arguments. image.png ** ② When defining the processing of multiple lines, enclose it in begin to end. ** ** In the example below, two lines are processed. image.png

Table of contents

Julia Quick Look Note [01] How to use variables and constants Julia Quick Look Note [02] Arithmetic Expressions, Operators [Julia Quick Note [03] Complex Numbers] (https://qiita.com/ttabata/items/225c77a4d71fafc3e482) Julia Quick Look Note [04] Regular Expression [Julia quick note [05] if statement] (https://qiita.com/ttabata/items/4f0bcff1e32f60402dfb) [Julia fast-drawing note [06] loop processing] (https://qiita.com/ttabata/items/2a53825101b0b75fb589) [Julia Quick Note [07] try, catch, finally] (https://qiita.com/ttabata/items/1d6fe990526c99b65b5f) [Julia Quick Look Note [08] Variable Types (Int, Float, Bool, Char, String)] (https://qiita.com/ttabata/items/2b84a826e39bfe432b62) [Julia Quick Look Note [09] Function (1) Basics] (https://qiita.com/ttabata/items/d9b4f2728ec0dbcc6394)

(* We will continue to increase the content)

Related information

: paperclip: Julia --Official page https://julialang.org/

: paperclip: Julia --Japanese official document https://julia-doc-ja.readthedocs.io/ja/latest/index.html

: paperclip: First time Julia and installation (Windows & Linux) https://qiita.com/ttlabo/items/b05bb43d06239f968035

:paperclip: Julia - Mathematics https://docs.julialang.org/en/v1/base/math/

Opinions etc.

If you have any opinions or corrections, please let us know.

Recommended Posts

Julia Quick Note [10] Function (2) Application
Julia Quick Note [09] Function (1) Basics
Julia Quick Note [06] Loop processing
Julia quick note [05] if statement
Julia Quick Note [04] Regular Expression
Julia Quick Note [07] try, catch, finally
Julia Quick Note [02] Arithmetic formulas, operators
Julia Quick Note [22] Calling Python functions and Python modules
Julia Quick Note [01] How to use variables and constants
Illustrated pandas function application process
Julia Quick Note [08] Variable Type (Int, Float, Bool, Char, String)
Function synthesis and application in Python