Jeffrey Sarnoff
Jeffrey Sarnoff

Reputation: 1757

Julia: How are keyword arguments used?

Please explain "keyword arguments" and give some examples of their use.

Upvotes: 12

Views: 7680

Answers (1)

Jeffrey Sarnoff
Jeffrey Sarnoff

Reputation: 1757

A function may use positional arguments and/or keyword arguments. Function arguments are used to provide functions with values developed outside of the function itself, values the function needs to know. For a given function, the actual value assigned to a specific argument may change from one call to the next.

A positional argument holds the place for specified value by its ordinal position in the function's argument list. For example, x and real, imag are positional arguments in these function definitions:

  • sqrt(x) = x^0.5
  • complex(real, imag) = real + imag*im

A keyword argument holds the place of a specified value by its name. The last (rightmost) positional argument comes before the first keyword argument. A semicolon (;) demarks the start of keyword arguments, even when there are no positional arguments. When both kinds of argurment are used, the semicolon separates the positional from the keyword arguments. For example, digitcount and digits are keyword arguments in this function definition:

  • roundedstring(x; digitcount) = string(round(x, digits=digitcount))

Here is an example that only uses keyword arguments:

function pathname(; dirpath, filename)
  return joinpath(dirpath, filename)
end

dir_path  = "/home/workfiles"
file_name = "current.txt"

path_name = pathname(dirpath=dir_path, filename=file_name)

# pathname == "/home/workfiles/current.txt"

Here is almost the same example, using both kinds of argument:

function pathname(dirpath; filename)
  return joinpath(dirpath, filename)
end

dir_path  = "/home/workfiles"
file_name = "current.txt"

path_name = pathname(dir_path, filename=file_name)

# pathname == "/home/workfiles/current.txt"

One reason to use keyword arguments:

function copyfile(; source, dest)
   # function body
end

src_pathname = "~/current.txt"
dst_pathname = "/home/workfiles/current.txt"

# now both of these work
copyfile(source = src_pathname, dest = dst_pathname)
copyfile(dest = dst_pathname, source = src_pathname)

Using a keyword argument to allow changing a default setting:

function translate(text; language="en")
   # translation function body
end

Using a keyword argument to require something:

#=
   If the keyword `language` is not specified
   when calling this function, a error occurs.
=#   

function translate(text; language)
   # translation function body
end

Both kinds of argument may have default values to use when an argument is omitted in a function call. All positional arguments that do not specify a default value must preceed all positional arguments that do specify default values. Keyword arguments have the same requirement, any with a default value must follow all keyword arguments that do not specify a default value.

Please see the docs for more information on keyword arguments and optional keyword arguments.

Upvotes: 22

Related Questions