<jupyter_start><jupyter_text>Functions in Julia I In this lecture- [Outcome](Outcome)- [Functions in Julia](Functions-in-Julia)- [Command line help](Command-line-help)- [Built-in mathematical functions](Built-in-mathematical-functions)- [Multiple dispatch](Multiple-dispatch) OutcomeAfter this lecture, you will be able to: - Say how a Julia function works- Describe the difference between built-in and user-defined functions in Julia- List and use some basic mathematical functions- Describe Julia's multiple dispatch using ``muladd()`` as the example [Back to the top](In-this-lecture) Functions in JuliaRecall a recent example using the funtion ``println()``:<jupyter_code>greeting = "Hello, world!!"
println(greeting)<jupyter_output><empty_output><jupyter_text>This is a good example of the basics of how a function works: it receives some input (in this case, the value of a variable) and makes something happen (in this case, showing a string on the screen).Many a function generates a value. In fact, possibly the most common use of functions is to create values to be assigned to variables:<jupyter_code>a, b, c = cos(0.2), log(10), abs(-1.22)  # multiple assignment to three separate variables
a<jupyter_output><empty_output><jupyter_text>Julia is a functional language, which means that in a sense the basic unit for organising code in Julia is the function. For instance, ``typeof()`` is a function that provides information which in other types of languages is obtained via other means. If you read the Julia code (it is publicly available, see www.julialang.org), you will see that virtually everything there is done via functions.This means of course that when you write code, you should also as far as possible organise your code in funtions. We address user-defined functions in Lecture 9 (the one after this one). [Back to the top](In-this-lecture) Command line helpTo get help in an IJulia, type "?" followed by a topic. For functions, you'll get the syntax (there may be several versions) and in other cases some brief guidance. A few example queries follow, concerning ```cos```, ```help``` and```floor```<jupyter_code>? cos # a standard maths function gives what you expect, with bonus for explaining what happens to arrays

?help   # space or not after after "?" makes no difference (but hopping to a new line does!)<jupyter_output><empty_output><jupyter_text>Ah well, asking for help in general isn't that helpful ... the right particular question gets good help, but they can be hard to find.<jupyter_code>? floor<jupyter_output><empty_output><jupyter_text>... while in this case the help may be a bit too detailed! It does alert you to the variety of outputs that ```floor``` can give you. Which one you get depends on the type signature of the variables you provide, and this is your first sight of one of Julia's greatest features: multiple dispatch. More on this feature below, though on this course you only glimpse its power (it is at the heart of much that makes Julia such a wonderful language). **This rapid, concise help is a wonderful resource to have on hand. USE IT OFTEN,    especially for spelling and syntax.** [Back to the top](In-this-lecture) Built-in mathematical functionsJulia has many built-in functions, and for scientific and technical computing the most important of these are mathematical. The following lists a very small part of what is available:``exp()````log()``    ...  NB! this is the *natural* logarithm``log10()``  ...    *this* is the logarithm to base 10``cos()``    ... argument must be in radians``sin()````tan()````acos()``   ... the result is a value in radians``asin()````atan(x)``   ...  NB! this only returns some angles (between -π/2 and π/2, to be exact)``atan(y,x)``  ... The *atan2* of many languages (including Python and Matlab); use this to get angles from quadrants two and three; it requires two inputs``floor()````ceil()````rem()````round()``  ... use round(Int, x) to convert any x of abstract type Real to an integer type (usually Int64)  **DO EXPLORE THESE USING THE "?" QUERY METHOD!** And if you enter the first one or more letters of a function, and press Tab, you'll see a list of possible completions. This includes all the available function names that start with that letter or letter. This is a good way to search for function names. For example:<jupyter_code>d      # Tab to show completions, use up and down arrows to scroll the list in box<jupyter_output><empty_output><jupyter_text>[Back to the top](In-this-lecture) Multiple dispatchThis is related to the fact that many functions allow, or even require, more than one input value. For example, ``binomial`` requires two values, and ``muladd()`` requires three (look them up!).Moreover, functions must accept inputs of more than one type. For instance, if we enter ``cos(1)`` then we are sending a value with type ``Int64`` to Julia's ``cos()`` function. And if we enter "``cos(1.)`` we are sending a value of type ``Float64``. But the value created by Julia's ``cos()`` function in both cases is 0.5403023058681398.Which is exactly what we want, of course. BUT!A function name points to a code body. When the code actually executes, it should be specialised on one type, else it will run very slowly, if at all. A function such as ``cos()`` actually has several such code bodies, to deal with inputs of different types. These are called the *methods* of that function. So how do Julia functions manage to have only one name, but many methods?The answer is multiple dispatch: Julia allows many methods, as long as they differ according to input type. All the input values are used to check this, hence the name multiple dispatch. The pattern of types in the input values in a given function call is its *type signature*. For example, ``muladd()`` has 12 permissible type signatures, which we see by using the function methods():<jupyter_code>methods(muladd)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0873229939312205, 'fraction_numerical': 0.0070802427511800405, 'mean_word_length': 4.874257425742575, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '295795', 'license': 'MIT', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1508, 'n_words': 860, 'path': 'Week1_8-Functions1.ipynb', 'repo_name': 'chs132/studious-octo-umbrella', 'size': '5932'}