CP- Creating Programs by Using Methods, Variables, and Expressions Overview
Creating Programs by Using Methods, Variables, and Expressions
You experimented with predefined functions in Processing in the last module. Remember, predefined functions have the code abstracted. Functions are a block of code that will perform an action or return a value. Functions are used in programming to break the code into pieces, making it easier to read or revise. They also reduce the amount of code the programmer must type and makes it easy to reuse. Once a function is created it can be called repeatedly.
So, what is the difference between a function and a method? It depends on the programming language. Java uses the term method to represent the block of code that performs an action or returns a value and Processing uses the term function. The process for creating a function in Processing and a method in Java is the same. Only the name is different. So, we will be using the term function while we are working in Processing, but the process and syntax will be the same for creating a method in Java.
Another important part of programming is the use of variables. Variables are used to store information about the program while it runs. Variables allow the programmer to write code and create programs that will work for any input and allows the code to change during the program.
One of the benefits of Processing is its ability to create active sketches very easily. In this module, you use two of the predefined functions setup() and draw(), declare and initialize variables and use mathematical expressions to create active sketches for animations and interactivity.
Essential Questions
- What is the difference between a function and method?
- What are the benefits of using functions or methods in a program?
- What is the difference between a global and local variable?
- What is the benefit of using variables rather than hard-coding values?
- Why is the order of operation important in mathematical expressions?
- How do you create and call a function or method? How do you create interactivity and animations in Processing?
Key Terms
Function -a function is a block of code used in processing that can return information or perform an action.
Method - a method is a block of code used in programming, specifically in Java, to perform a specific action or task or return information.
Data Type - represents a particular type of data that is used in combination with variables to define what type of data the variable will store.
Variable -a variable stores a value in memory so it can be used later in a program.
Hard-coding - to use an actual value rather than a variable for something that is likely to change at a later time.
Mathematical Expressions - a combination of operators and operands to perform calculations.
I[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION