 So multiple dispatch What really happens when we call a function? Well, actually a whole bunch of them are created Not just a single function. We declare function function give it a name Tell about the arguments, but there's actually quite a few of them get created and each one of them has to do with the Data type the type of the argument that you pass the type of the value that goes into the argument So a whole lookup table gets created with every function stored with the function and When you generate code with an argument Julia will look at what the type is of that argument and that's only for the ordered arguments I'm gonna do that and it will decide which one of the functions to use and those are called which one of the methods of the function to use So when I create this function CBD for cube for instance a and it returns a cube. I can pass anything into that a I can post post an integer and a float and Each one of those are actually a different function So there's all family of these little functions that are the different Implementations of that function and these are really called methods So when I call this function say functions with an integer that Julia will generate code that uses the computer's central processing units integer multiplication instruction set or the for a floating point value the floating point multiplication instruction set specific to that computer and The implementation of of a function based on arguments that is what is called multiple dispatch And you see it prominently on the Julia's web page on the front page there functions with multiple dispatch So again, remember it's only for the positional arguments that it's going to do that look up of which method to use And when I use that function again, and I use different different data type a New method will be called and that process is called overloading So let's look at the methods that can be called for the plus function again I can just use the code methods and pass the argument plus it'll take a while look Booleans to Booleans an abstract float in a Boolean and integer and an integer So these are all different methods for that plus function good so I Can make use of this plus two and that because it's a real number and a complex number And that is one of the defined methods for the plus function If I put plus and I put two strings there though that won't work because that method is not defined for the plus function Strange enough it is though for the multiplication Function that is one of the methods does take a string in a string and it's just going to concatenate them I love Natala just like that Now we can also do something else we can we can we can Call a function and we're going to see which method is used and we use this macro at which So it's a multiplication two and two these are two Booleans So we can clearly I can guess at what which of the methods of this multiplication function is going to be used in this instance Two and two days are both Booleans so X is a Boolean and Y is of type Boolean I can also ask what functions have integers as At least one of there at least one of their methods And that's going to take a while because there are many in-built Julia functions and Quite a few of them as I would suspect most of them at least will have an integer name You see 730 element arrays take integers as one of the methods So that's beautiful. So how can you use that? It's very powerful because you can constrain put some constraints on your function So I'm going to create this function my underscore funk and you see I've created it one two three four times here and Every time it's a function with an end and it returns something It's the same name though But it means I'm going to it's an overloading really I'm using multiple dispatch to create multiple methods of this function So that when I pass these as arguments there is a method for that So if I just write a comma B that is generic and it really implies that master Type called any so any of the sub types of any can be passed And if if Julia finds that when I call this function for my two arguments It's going to just return this print function generic case I'm doing my funk again And I'm gonna say a as a real number B as a real number and then it'll say both arguments are real numbers Is real just comma B B is then any or a and B Be being real a being any so let's run that Let's run that and let's pass two numbers my fun pie comma three My fun pie comma three. Let's do that and it says both arguments are indeed numbers So that came from your both arguments are numbers because they both real It's going to find whatever is more appropriate So they are both real is a sub type of any but real is like the first hurdle that it gets to So that's what it's going to return if I return this which is a number and Julia which is just a string it says the first argument is a number and that's what it did here It said a is is real and B could have been any and string is a sub type of any So that's the closest method or the method that was used to execute the calling of that function and If I pass Julia and then exponent one it says the second argument is a number I love Julia now I did not have a method that was string string But I do have with this generic case where it could be anything and string is definitely aske string is a sub type of any So it was going to use the generic case and now For this if I just look at the method it says it's a generic function my funk with four methods Remember we've always just seen generic function with one method now. You know what it's all about Now usually we just pass some ordered arguments and it was this any Because we didn't specify that's how we landed up with one method But yeah, we've created this function and it actually has we have created four methods for that is lovely now Let's just look at a few in both Julia functions functions just for Just for pleasure and we've done it before I use it every time plus is a function So I need to write two plus two I can call the function plus and I can pass two arguments to it We've seen We've seen what that is and just to prove the fact I'm can ask what is the type of plus indeed it is just a function Filter is another nice function to use just show you they are so many men touch on one or two year filter I it's now the stabby function And it's just going to filter for is even is even is also Is also a Inbuilt function is just going to test if a value that's passed to it is even and I'm going to pass to it To this filter now remember the filters is going to return booleans. So yes or no and then if it is true I should say true or false if it is true, then it's going to return that value that's held in I And that takes the stabby function and the second argument which is my array my rays from 1 to 20 So it's going to go one. It's going to ask is it even it's going to return a false because it's not even and that gets ignored When it gets to 2 it says is even yes, it's true And the filter will then let it through So as you can well imagine, it's just going to return for me 2468 until 20 Find an is prime is to another very useful one So I'm going to define this function called prime underscore numbers I'm going to say it should be of type integer any of integers subtypes Such that I have this argument a which is an array of T values and it's a one column array as a one column vector That's what it's saying there. Okay. So let's do that and what it does is it uses inbuilt function called find is prime a But difficult just to look at it like that. Let's see. Uh, let's just see there This is from that and then I'm going to call prime numbers and I'm and I'm going to run it from 1 to 20 so that you can see So that's why I said Julia doesn't think that one is a prime number and is is one a prime number It can provide by one in itself, but one is itself. So is it a prime number? um So it's going to go from 1 to 20 And it gets passed there This is a an array with just one column All of them are 64 bit integers. There's just one column. It's a vector actually a column vector From 1 to 20. It's not a matrix of 2 by 2 3 by 2. It's just one single column. That's what that one is for They're all integers and it's an array and that is what what I said there it must be an array and It must only be one column. That's the only thing that it's going to accept And each and every one of these elements as specified by the type There has got to be an integer or sub type of integer. So it certainly couldn't pass the bunch of of Irrational numbers there a bunch of floating point values and the function prime Is find so again, it's going to return true or false true Then it will give me the value false. It will just ignore and there is this is prime Is prime function and just running through each of one each individually of those values Now there's we can simplify that function actually we can just call it function easy primes a is an array Such that it's all into the 64 elements and it must be a single column array And it just find is prime a as we've seen before In our function and if we run easy primes now again, we're going to get exactly the same result Very lastly, let's just do some less comprehension not really part of functions, but Always fun to do they really crop up everywhere. We've seen them before But let's just take a quick look right to end off this very nice section of functions. This has as an added bonus So I'm creating this matrix And what does it do? I'm going to call it matrix one and it is x plus y for x in one to two and y in one to three This is run that and you're going to see what gets returned as two by three array. So what's happening here? So x is going to be this one and one to two and y one two and three So it's going to say one plus one that gives me the two Then one plus two so one and x the next one and y So you can see the order in which this gets executed that gives me three and one plus three is four Now it's going to jump to the two and the x for the next row So two plus one is three two plus two is four two plus three is five So that is lovely now just to remember you don't have to use this in keyword You can also use the equals keyword that's going to give me exactly the same thing And for a bit of fun, let's create back to school create the 12 by 12 table So it's x times y for x in one to 12 y in one to 12 quick and dirty if If someone comes to you asking for the really quickly for the 12 times 12 table there you go No problem at all. So there's a bit of a mix of less comprehension That's building up her values building up an array building up a matrix You can use this part of functions here, but we've had a Good nice look at functions now They're absolutely powerful and there's this multiple dispatch of various methods in the function that really make them powerful And you can take absolute control of that I really hope you enjoyed this lecture have some fun create your own functions They are really powerful in julia