Part 4 - Constants
Constants are types of data that cannot be changed by external means. It also allows us to easily replace variables should requirements change.
let's look at this example.
Let's say that we want to make an Image. Here, we have defined WIDTH and HEIGHT as constants which points to 400 and 600 respectively. The convention for declaring constants are Upper case in most programming languages.
Constant declarations do not produce any output.
If we define WIDTH and HEIGHT in our code, this would be replaced by 400 and 600 respectively.
Here is how racket evaluate constants.
(define WIDTH 400)
(define HEIGHT 600)
;CONSTANT DECLARATIONS
(* WIDTH HEIGHT)
(* 400 HEIGHT)
(* 400 600)
Racket replaces the constants with their respective values.
Interesting enough, we can use constants with Images. Go to http://www.ccs.neu.edu/home/matthias/HtDP2e/part_one.html and scroll down where you would find the cat image.
Copy the image and paste it to the IDE. Since images are values. You can set it as a constant.
There is a primitive that manipulates images to rotate them.
Interestingly enough, we can use entire expressions as a constant to simplify things if we are going to reuse a certain statement
The constant would be replaced by the respective expression.
Mga Komento
Mag-post ng isang Komento