Page 102 - Learn To Program With Scratch
P. 102

100


                                                                         The parameter side
                                                                         is set to 100.
                                   The value 100 is passed
                                   to the Square procedure.



                         Figure 4-17: Calling the Square procedure with side set to 100
                             Here, the number 100 (called an argument) is passed to the Square
                         procedure. When Square is executed, its side parameter is set to 100, and
                         this value is used to replace all occurrences of the side block inside the pro-
                         cedure. As you can see, the ability to specify different arguments to a proce-
                         dure is a powerful feature that adds a lot of flexibility to our programs.
                             We can enhance our Square procedure even further by making it accept
                         the square’s color as a second parameter, as shown in Figure 4-18. Here, we
                         added a second input parameter, called clrNum (short for color number),
                         which indicates the desired color of the square. The procedure now sets the
                         pen color to the value specified by clrNum before executing the drawing loop.
                         Edit the Square block to implement the changes shown in the figure.




                                           Par ameterS VS. argumentS
                             Although many programmers use the terms parameter and argument inter-
                             changeably, the two terms are in fact different . To clarify, consider the Average
                             procedure shown below, which computes the average of two numbers .

                                                                          num1 and num1
                                                                          are parameters
                                                                          to the procedure.
                                  The numbers 100
                                  and 50 are called
                                  arguments.


                                As defined, this procedure has two parameters named num1 and num2 .
                             A parameter defines an input to a procedure . You’d call this procedure with the
                             block shown at the left and specify some values or expressions inside the avail-
                             able slots . The values 100 and 50 in the above example are called arguments
                             of the procedure .
                                Of course, the number of arguments in the procedure call must match the
                             number of parameters in the procedure’s definition . When you call Average,
                             the parameters num1 and num2 receive the values 100 and 50, respectively,
                             because arguments and parameters are matched by position .







           80   Chapter 4
                                           www.it-ebooks.info
   97   98   99   100   101   102   103   104   105   106   107