Julia Tutorial - 5. Functions

codebasics · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

Explains Julia functions

Full Transcript

hello and welcome to code Basics coding tutorial today's topic is functions in Julia now we all programmers love functions very much they make our life very easy so let's talk about them and here is a list of items we are covering in this video okay as usual I'm going to use Julia box do org for this tutorial we are going to type it in into Julia notebook uh if you have Julia terminal install on your local PC you can use that as well okay so I'll sign in via Google and I will create a Julia notebook by going to new menu click on the latest version I'm going to click on this one okay let's jump straight away into writing our first function the way you write function in Julia is using function F so that's the name of your function enter and let's say this sum just this function just does the sum of two numbers so I will say return x + y and to end the function you have to use and keyword this indicates the function body is now complete it it has ended okay Alt Enter to execute any line in Julia notebook as usual okay so this Define our first function now let's call it excellent so it gives this result back now uh if you are new to programming uh just to cover the definition of function function is nothing but a code block that performs a well defined task here that well defined task is adding two numbers so this code block performed that function and now you can call F this function any number of times to execute this code okay now there is another compact way of defining the same function which is this you can do f f of XY = x + y this will also do the same thing so it's B essentially the same it has the same effect it's just a very compact way of writing a small function now of course if your function is Big you don't want to do it this way but if it is small like adding two numbers then you can certainly go with this option okay now in Julia you don't need to always return a value it will by default return the value of the last line of execution in your code so if I Define the same function f and I just do uh sorry I I hit Alt Enter that's why you have to hit enter and I will just do x + y and N now notice that I'm not returning a value here here I was returning it uh let's see what happens nice so even if you don't return a value here by default it will always return a value of last line that it executed in that function body okay U now if you explicitly return a value then it will stop execution after that point so for example again I'm going to write the same function and I'm going to return a value saying x + y and then I do X multiplied by y okay and if I do if I call that function again notice that it is giving me sum as an output so it executed this line return x + y after that it didn't went any further so this line had no meaning okay so once you explicitly write return it will always return from that point if you don't write return let's say if you don't write this then it will always execute this one so for example if my function body was like this let me quickly show you and if I do this then it is going to go down and it is going to multiply these two numbers and since this was the last line in the function that's what it return back okay now how do you return multiple values to return multiple values you can do something like this again the same function and I'm going to return two values now which is a + b and a multiplied b as I told you before I don't need to write return statement it will always return so to return multiple values you just return them by separating them by comma okay when I do that and if I call it again interesting so it returns a tle back tupple means uh sequence of multiple values so here the first one was the sum of these two number which was three and four was seven that's why I got seven here the second one was multiplication between the two arguments so 3 into 4 was 12 and that's what I got back you can also do something like this that way you store the value of those two return values into two distinct variables so when I I do now X it says 7 and Y will uh show me 12 the next item is operators are functions what does it mean when you do any operation like this you're this plus here is called an operator it is an addition operator okay uh now this operator right here is also a function so you can do the same thing using this syntax so as if I'm calling this function so plus is the operator which is also a function and you can call it using the function syntax and it does the same thing same thing with the multiplication operator you can multiply by two number by this or you can use the function syntax like this if you really want to call a function by some English name then you can assign operator to that English string so if I want to say f equal to plus okay and if I say f 2 and three it's going to do sum of those two functions now as you notice here I can assign operator which is a function to a variable name what this means is functions are first class objects in Julia uh so you can actually assign function to a variable so let's do that for example our last function so I'm just going to Simply Define the same function again and I'm going to now let's assign that function to a variable called G so you you treated literally f as your first class object and assigned to a variable so now when you use that variable to call a function it will work okay uh second thing is function can also be used as a uh function argument okay so you have a function that you use an argument for another function and the standard example for that is the map function so map function in Julia what it does is it takes the first argument of this function is a a function and the second argument is a list so for example if I have a list here list or an array that is that will be my second argument and my first argument will be function itself so I can literally Define a function like this okay and I can do something like x * by 2 let's say I I have this array and I want to multiply all the elements of this array with two and return back uh the second array which has all those results in it so you can do something like this and when you say all enter what just happened here is it went through all the elements in the array and it then treated each of these elements as X here and it multiplied them by two and the return value was another array with all all those results filled in this helps whenever you want to do a quick transformation on one array and generate the second array okay so as you noticed function was used as an argument to another function similarly uh a function can be return as a return value from any other function um that's why uh functions are first class object section Julia another way of doing the same thing will be to use this syntax where you will say x and hyphen Arrow XA 2 one 2 3 this will have the same effect but it is more compact format it is same as this one but little bit uh compact okay uh the next item is optional arguments uh whenever you're calling any function sometimes you don't want to supply one or the other argument so our standard function which does the sum of two number let's say if you don't want to supply the second number then this is how you uh initialize a default value of an argument what it means is if I when when I call F function and if I don't Supply the second argument then by default it will take the value to be five okay x + y and now if I call it with the second argument then this five will not have any impact okay so when I do this is going to show me 7 it's not going to show me8 right like 3 + 5 8 it's not like that but if I don't apply the second argument which I'm going to do right now then it will be eight because the three went into X and there was no Y and it used five as the value of y okay uh the last item is the keyword arguments sometimes when you're are defining a function the argument list is pretty long okay now until now we were using the position argument arent position argument means X and Y if you look at these two arguments X is the first argument Y is the second one and we are using position here so three since it is first that goes into x four since it is second it goes into y but if your list is too long it might be cumbersome you have to see you know like what three means 3 means x 4 means y you have to kind of map that uh if you don't want to do that then you can use something called a keyword argument so let's define our function which is a division with a keyword argument now the way you specific keyword argument is you start with a semicolon and then you uh write down your keyword arguments and the important thing here is you have to always initialize uh your keyword argument with a default value okay uh let's do this so this function just divides the first number by the second number now when you normally call it okay uh when you say x = to 10 or let's say xal 20 and Y is = 10 okay when you do that it works so X is 20 Y is 10 now it will also work even if the order of these two arguments is different because now we are explicitly saying that 20 is X and 10 is y so the order here doesn't matter so see it gives you the same result okay so that was all about functions in Julia thanks for watching bye-bye

Original Description

Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Website: http://codebasicshub.com/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 24 of 60

1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related Reads

Up next
What is Telematics Explained with Examples
VLR Software Training
Watch →