1 / 32

11 – Parameters

11 – Parameters. Question: Arrays. How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2) = 21. 1. Question: Arrays. How many errors are in the following code: Dim x Dim y Dim f(4) f = 3 y(3) = 6 f(1) = 19 f(5) = 21. Session Aims & Objectives.

Download Presentation

11 – Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 11 – Parameters

  2. Question: Arrays • How many array variables are in the following code:Dim xDim yDim f(4) x = 12 y = 6 f(2) = 21 1

  3. Question: Arrays • How many errors are in the following code:Dim xDim yDim f(4) f = 3 y(3) = 6 f(1) = 19 f(5) = 21

  4. Session Aims & Objectives • Aims • To introducepassing parameters/arguments to procedures to make them more general purpose • Objectives, after this week’s sessions, you should be able to: • use parameters in your programs to make procedures more flexible • distinguish between formal and actual parameters • pass parameters by reference and by value • identify and correct parameter errors

  5. Example: Clock (analysis) • SPECIFICATION • User Requirements • to know what the time is • Software Requirements • Functional: • display current time in analogue (traditional clock face) format • Non-functionalshould be easy to read

  6. Example: Clock (user interface) • 14 objects: • 12 numbers • mid point • second point (moving)

  7. Example: Clock v0 (HTML) <html> <head><title></title></head> <body> <div id=div1 style="position: absolute;">1</div> <div id=div2 style="position: absolute;">2</div> <div id=div3 style="position: absolute;">3</div> <div id=div4 style="position: absolute;">4</div> <div id=div5 style="position: absolute;">5</div> <div id=div6 style="position: absolute;">6</div> <div id=div7 style="position: absolute;">7</div> <div id=div8 style="position: absolute;">8</div> <div id=div9 style="position: absolute;">9</div> <div id=div10 style="position: absolute;">10</div> <div id=div11 style="position: absolute;">11</div> <div id=div12 style="position: absolute;">12</div> <img id=imgMid style="position: absolute;" src="dot.gif" /> <img id=imgSec style="position: absolute;" src="dot.gif" /> </body> </html>

  8. Example: Clock v1 (HTML) • Remove 'hard coded' tags <html> <head><title></title></head> <body> <img id=imgMid style="position: absolute;" src="dot.gif" /> <img id=imgSec style="position: absolute;" src="dot.gif" /> </body> </html>

  9. Example: Clock v1 (code) • use loop to put in tags Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 div3.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 3) * 200 div3.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 3) * 200 div4.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 4) * 200 div4.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 4) * 200 div5.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 5) * 200 div5.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 5) * 200 div6.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 6) * 200 div6.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 6) * 200 div7.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 7) * 200 div7.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 7) * 200 div8.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 8) * 200 div8.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 8) * 200 div9.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 9) * 200 div9.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 9) * 200 div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style='position: absolute;'>" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 … … … div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setInterval "ShowHands", 500 End Sub

  10. Example: Clock v1 (code) Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 div3.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 3) * 200 div3.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 3) * 200 div4.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 4) * 200 div4.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 4) * 200 div5.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 5) * 200 div5.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 5) * 200 div6.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 6) * 200 div6.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 6) * 200 div7.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 7) * 200 div7.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 7) * 200 div8.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 8) * 200 div8.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 8) * 200 div9.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 9) * 200 div9.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 9) * 200 div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub current time and date Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub

  11. Example: Clock v1 (code) Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 … … … div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 div3.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 3) * 200 div3.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 3) * 200 div4.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 4) * 200 div4.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 4) * 200 div5.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 5) * 200 div5.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 5) * 200 div6.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 6) * 200 div6.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 6) * 200 div7.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 7) * 200 div7.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 7) * 200 div8.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 8) * 200 div8.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 8) * 200 div9.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 9) * 200 div9.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 9) * 200 div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub

  12. Example: Clock v1 (code) • same 2 lines duplicated • not identical • different object • different angle Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 div3.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 3) * 200 div3.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 3) * 200 div4.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 4) * 200 div4.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 4) * 200 div5.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 5) * 200 div5.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 5) * 200 div6.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 6) * 200 div6.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 6) * 200 div7.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 7) * 200 div7.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 7) * 200 div8.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 8) * 200 div8.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 8) * 200 div9.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 9) * 200 div9.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 9) * 200 div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub

  13. Problem: Inflexible Procedure • Procedure does exactly the same each time Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) Position End Sub Sub Position() imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub

  14. Parameters/Arguments: What Milk Make cup of tea Sugars • Sometimes procedures need information • Making a cup of tea: • milk, and number of sugars • Makes them more flexible

  15. Parameters: How • Procedure Declaration • formal parameters define name • Procedure Call • actual parameters list values in order Option Explicit Dim res Sub Calc(Num1, Num2) res = Num1 * Num2 End Sub Sub btnCalc_Click() Calc 5, 2 Calc 11, 15 End Sub

  16. Parameters: How

  17. Error: Wrong number of arguments Option Explicit Sub Twice(num) parResult.innerText = num * 2 End Sub Twice 3 Twice 4, 3 Twice

  18. Questions: Parameters • Name the procedures in the following code and state how many parameters they take: Sub hello(s, x) Dim i s = "" For i = 1 To x s = s + "Hello" Next End Sub Sub DisplayScore() parScore.innerText = score End Sub hello takes 2 parameters DisplayScore takes 0 parameters

  19. Questions: Parameters Given the following declaration: • What will the following put in parRes? Sub thing(Num1, Num2, Num3) Dim tmpOutput tmpOutput = (Num1 + Num2) * Num3 parRes.innerText = tmpOutput End Sub thing 2, 3, 6 thing 6, 3, 2 thing 20, 5, 2 30 18 50

  20. Example: Clock v2 (code) Position div1, ang Position div2, ang * 2 Position div3, ang * 3 Position div4, ang * 4 … … … Position div9, ang * 9 Position div10, ang * 10 Position div11, ang * 11 Position div12, ang * 12 Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 Position div1, ang Position div2, ang * 2 Position div3, ang * 3 Position div4, ang * 4 Position div5, ang * 5 Position div6, ang * 6 Position div7, ang * 7 Position div8, ang * 8 Position div9, ang * 9 Position div10, ang * 10 Position div11, ang * 11 Position div12, ang * 12 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) Position imgSec, ang * s End Sub Sub Position(objO, a) objO.style.pixelleft = imgMid.style.pixelleft + Sin(a) * 200 objO.style.pixeltop = imgMid.style.pixeltop - Cos(a) * 200 End Sub Position imgSec, ang * s Sub Position(objO, a) objO.style.pixelleft = imgMid.style.pixelleft + Sin(a) * 200 objO.style.pixeltop = imgMid.style.pixeltop - Cos(a) * 200 End Sub

  21. Comparison of Clock Code v0 (64 lines) v1 (59 lines) v2 (51 lines) (2474 bytes) (2064 bytes) (941 bytes) <html> <head><title></title></head> <body> <div id=div1 style="position: absolute;">1</div> <div id=div2 style="position: absolute;">2</div> <div id=div3 style="position: absolute;">3</div> <div id=div4 style="position: absolute;">4</div> <div id=div5 style="position: absolute;">5</div> <div id=div6 style="position: absolute;">6</div> <div id=div7 style="position: absolute;">7</div> <div id=div8 style="position: absolute;">8</div> <div id=div9 style="position: absolute;">9</div> <div id=div10 style="position: absolute;">10</div> <div id=div11 style="position: absolute;">11</div> <div id=div12 style="position: absolute;">12</div> <img id=imgMid style="position: absolute;" src=dot.gif /> <img id=imgSec style="position: absolute;" src=dot.gif /> </body> </html> <script language=VBScript> Option Explicit Sub window_OnLoad() Dim ang imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 div3.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 3) * 200 div3.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 3) * 200 div4.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 4) * 200 div4.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 4) * 200 div5.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 5) * 200 div5.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 5) * 200 div6.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 6) * 200 div6.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 6) * 200 div7.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 7) * 200 div7.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 7) * 200 div8.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 8) * 200 div8.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 8) * 200 div9.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 9) * 200 div9.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 9) * 200 div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub </script> <html> <head><title></title></head> <body> <img id=imgMid style="position: absolute;" src=dot.gif /> <img id=imgSec style="position: absolute;" src=dot.gif /> </body> </html> <script language=VBScript> Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 div1.style.pixelleft = imgMid.style.pixelleft + Sin(ang) * 200 div1.style.pixeltop = imgMid.style.pixeltop - Cos(ang) * 200 div2.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 2) * 200 div2.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 2) * 200 div3.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 3) * 200 div3.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 3) * 200 div4.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 4) * 200 div4.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 4) * 200 div5.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 5) * 200 div5.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 5) * 200 div6.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 6) * 200 div6.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 6) * 200 div7.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 7) * 200 div7.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 7) * 200 div8.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 8) * 200 div8.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 8) * 200 div9.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 9) * 200 div9.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 9) * 200 div10.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 10) * 200 div10.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 10) * 200 div11.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 11) * 200 div11.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 11) * 200 div12.style.pixelleft = imgMid.style.pixelleft + Sin(ang * 12) * 200 div12.style.pixeltop = imgMid.style.pixeltop - Cos(ang * 12) * 200 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) imgSec.style.pixelleft = imgMid.style.pixelleft + Sin(ang * s) * 200 imgSec.style.pixeltop = imgMid.style.pixeltop - Cos(ang * s) * 200 End Sub </script> <html> <head><title></title></head> <body> <img id=imgMid style="position: absolute;" src=dot.gif /> <img id=imgSec style="position: absolute;" src=dot.gif /> </body> </html> <script language=VBScript> Option Explicit Sub window_OnLoad() Dim ang Dim s Dim i s = "" For i = 1 To 12 s = s & "<div id=div" & i & " style=""position: absolute;"">" & i & "</div>" Next document.body.innerHTML = s & document.body.innerHTML imgMid.style.pixelleft = document.body.clientwidth / 2 imgMid.style.pixeltop = document.body.clientheight / 2 ang = 6.2 / 12 Position div1, ang Position div2, ang * 2 Position div3, ang * 3 Position div4, ang * 4 Position div5, ang * 5 Position div6, ang * 6 Position div7, ang * 7 Position div8, ang * 8 Position div9, ang * 9 Position div10, ang * 10 Position div11, ang * 11 Position div12, ang * 12 window.setinterval "ShowHands", 500 End Sub Sub ShowHands() Dim ang Dim s ang = 6.2 / 60 s = Second(Now()) Position imgSec, ang * s End Sub Sub Position(objO, a) objO.style.pixelleft = imgMid.style.pixelleft + Sin(a) * 200 objO.style.pixeltop = imgMid.style.pixeltop - Cos(a) * 200 End Sub </script>

  22. Example: Clock v3 • Use for loop instead of repeated calls Position div1, ang Position div2, ang * 2 Position div3, ang * 3 Position div4, ang * 4 Position div5, ang * 5 Position div6, ang * 6 Position div7, ang * 7 Position div8, ang * 8 Position div9, ang * 9 Position div10, ang * 10 Position div11, ang * 11 Position div12, ang * 12 For i = 1 To 12 Position document.getElementById("div" & i), ang * i Next

  23. Example: Clock v4

  24. Memory Addresses • Memory addresses - keep track of where variables are stored in memory • Every byte (position) in memory has a memory address: • e.g. variable identified by name xstored at location 63542(address of the first byte of data for variable x) Computer Memory 0 63542 x 23 Identifier Value

  25. Example: Change the Value

  26. Parameter Passing Methods • 2 ways to pass parameters to procedures: • Passing by Value: literalvalue is passed from the call to the definitionSub Change1(ByVal x) …End Sub • Passing by Reference: variable’s memory address (a reference to the variables position in memory) is passed from the call to the definitionSub Change2(ByRef y) …End Sub

  27. Why pass by reference? Change Set ByVal x ByRef x • Previously, used parameters to pass data into a procedure (input) • Pass by reference, allows: • the procedure to change the value of the passed variable • data to be input and output via parameters

  28. What can be passed • Pass by value – both literals and variables can be passed (variables are substituted by their value)Change1 y Change1 21  • Pass by reference – only variables can be passed (in fact the variable’s memory address is passed)Change2 y literals cannot be passed – they have no memory address Change2 21  ERROR

  29. Two results? integer (total) TotAve double (average) • Total and Average algorithms share a lot of code • Useful to combine them • TotAve procedure:

  30. Example: Total and Average Sub TotAve(ByRef T, ByRef A) Dim i T = 0 For i = 1 to 5 T = T + Nums(i) Next A = T / 5 End Sub tot = 0 ave = 0 TotAve tot, ave ' What is the value of ave and tot? var T TotAve var A

  31. Questions: Parameter Passing • Consider the following code: Dim a Sub Add(ByRef item, ByVal extra) item = item + extra End Sub a = 5 • What is the value of a, after each of these: Add a, 3 Add a, a Add 4, 4 8 16 Error – cannot pass 4 by reference

  32. Tutorial Exercise: Clock • LEARNING OBJECTIVE: • use parameters provided by event procedures • Task 1: Get the Clock (v2) example (from the lecture) working. • Task 2: Modify your code – display lots of dots for the second hand (as per v3). • Task 3: Modify your code – add an hour and minute hand

More Related