Writing Your Own Function
1. Writing Your Own Function
Congratulations on getting close to the end of this chapter! In this video we will quickly recap structs, and then introduce functions in structs. We will bring together everything you have covered so far.2. Structs recap
Before we introduce a new concept in structs, let's quickly recap what we have already seen. Remember struct is the keyword used to define a composite type, where the type can contain as many fields as we want. We can think of this composite type as a blueprint for an object. In this example, we have created a mutable struct with four fields, and each field has a type associated with it. The struct House forms a blueprint, and we can then use this blueprint to create an object. We then create an object my_house, passing in four values. Constructors are functions that create new objects, or more specifically, instances of composite types. In our example here, a constructor would be an instance of our composite type House. A struct is a concept that exists in other programming languages as well, and is broadly referred to as part of Object Oriented Programming, one of the common programming paradigms. Courses dedicated to Object Oriented Programming exist on DataCamp for other languages, and you will commonly see Object Oriented Programming in real-world problem solving.3. Functions and custom structs
As we have already seen, structs are very flexible and can be customized according to our needs. In this example, we have our mutable struct Person that we can recall from the previous exercises. This mutable struct has three fields with data types attached, but most importantly, we have also created a new constructor within our mutable struct. This constructor is the function within the struct definition. This constructor means that we only require the age and the height when creating an instance of our struct, and the location will always be set to London. We can test this out by creating steve, an instance, and we can see that even though we have only passed the age and height, the location of the instance is already set to London, thanks to the constructor within the mutable struct.4. Functions recap
In the past few exercises we've seen how to pass different arguments into a function, how to pass varargs into a function, and how to return output from a function. We've also seen in the previous chapters how to handle various data structures, storing and retrieving data from a range of iterable structures. The key takeaway is that functions are flexible, and being able to write your own functions to solve a practical, real-world problem is an essential part of programming. In this final lesson, you'll be writing two different functions in two exercises. To complete these exercises, you will need to draw on the knowledge of functions in Julia to write an appropriate solution to the problem.5. Let's practice!
It's time to write your own functions. Good luck!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.