[PYTHON] Julia Quick Note [09] Function (1) Basics

Function (1) Basics (Example of how to write)

note09



function sum(a, b)
    return a + b
end

◆ When return is omitted
 function sum(a, b)
     a + b
 end

◆ One-line display
 sum(a, b) = a + b

◆ Multiple return values
 function sum(a, b)
   x = a + b
   y = a - b
    return (x, y)
 end

Commentary

(1) The basic format starts with function and ends with end. The value returned by the function is represented by return. (2) The return clause can be omitted. In this case, the last evaluated value will be the return value. (3) A function can be expressed in one line. In this case, the function and end clauses are unnecessary. Also, it cannot be written over multiple lines.

▼ Example of how to use the one-line display function (see the figure below) image.png (4) Multiple return values can be returned using the tuple type. Even if the return value is 3 or more, it can be returned using tuples.

▼ Check the type of return value (plural) (example with two return values) 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 [09] Function (1) Basics
Julia Quick Note [10] Function (2) Application
Julia Quick Note [03] Complex Numbers
Julia Quick Note [06] Loop processing
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
Julia Quick Note [08] Variable Type (Int, Float, Bool, Char, String)