Basic Functions
1. Basic Functions
Functions are a way for us to organize our code and reuse parts effectively. In Julia, functions are particularly important as the same code placed inside a function can run faster than that code outside a function.2. What are functions?
You have been using functions throughout this course. Here's a reminder of just how many you've used. A function is something that takes an input and produces an output. For example, the length function takes an array and returns an integer describing the length of that array. The sort function takes an array and returns a sorted version of that array. The print function takes a value and outputs it to the console.3. Why use functions?
Functions allow us to abstract pieces of code to focus on our program's bigger picture. For instance, once we have a sort function that works well, we can forget about any of its inner workings and move on to other parts of the code.4. Writing custom functions
Here's an example of how to write a function in Julia. We begin with the function keyword followed by the name we want to give it. This function uses one argument, which is named temp. For this function, temp should be a number representing the temperature in Fahrenheit. We call the inside of the function the function's body. There, we convert Fahrenheit to Celsius and return the new value. We must indicate the end of the function with the end keyword. After we have completed the function, we can use it in our script.5. Writing custom functions
Because we have written the calculation of converting temperatures into a function, we reuse it again and again.6. Longer functions
Inside the function's body, we can have multiple lines of code. This is useful when the function is more complex, and we need to break it down into steps. Only the value which follows the return keyword is output by the function.7. Longer functions
The variables defined inside the function are not available after the function has run.8. Return keyword
We can use multiple return statements inside a function. In this function, we use an if-statement to decide what will be returned. If x is positive, then the value of x is returned. If it is negative, then zero is returned.9. Return keyword
A function doesn't need to use the return keyword. This function doesn't return any value; it only prints.10. Multiple arguments
Functions may take multiple arguments. This function takes two values, x and y, and returns x to the power of y. When using multiple arguments, the order of the input can be very important. In this example, if we reverse the input arguments, we calculate a very different value.11. Broadcasting functions
The functions we have seen only work with single input values, but Julia has a neat trick to run any function on each element of an array using the dot-syntax. This function doesn't normally work on arrays.12. Broadcasting functions
If we place a dot between the function name and the parentheses, Julia will apply the function to each array element independently and return a new array of all of the output values.13. Broadcasting functions
We can broadcast with any function. Here we use the dot-syntax with the typeof function to find the data type of each element in this array.14. Broadcasting multiple arguments
We can also use the dot-syntax to broadcast a function with multiple arguments. In this case, only one of the arguments is an array, so each array element will be squared.15. Broadcasting multiple arguments
If both arguments are arrays, Julia will apply the function using pairs of elements from matching index locations.16. Let's practice!
We've learned a lot about Julia functions, let's practice.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.