Functions
What you will learn
- What functions are and how to call them
- How to create your own custom functions
- How to pass data to a function
- How to loop the same code multiple times
- How to return data from functions
- How to use custom functions to create your own art project
Common issues
- One thing you might forget to do is call your custom functions once you've created them. Remember that a function is just a blueprint for a chunk of code that could be run. To actually run the function, you must call it using its name.
- When adding parameters, it is also easy to confuse the syntax for the function definition and the function call. Remember that when you define a function with parameters, you must give the parameter name and type. However, the function needs to be given specific values for the parameters when it gets called. These values can be different every time the function is called.
// Define a function
func someFunction(parameter1: String, parameter2: Int) {
// Do something
}
// Call the function with parameter values
someFunction(parameter1: "Hello World!", parameter2: 100)
- Be sure to complete all of the shape examples on each page to help cement the syntax of functions and loops. Together they will make up the foundation of most of your future code!
What you should understand when finished
- What is the purpose of using functions in your code?
- How do you create and call a function?
- How can you repeat code using loops?
- How do you pass data into a function?
- How can you return a value from a function?