{"base":{"id":"cs9-05","subject":"Algorithms","title":"Calling a routine","scene":"code9","steps":[{"mode":"call","eyebrow":"Procedures","title":"Jump in, run, jump back","body":"A procedure is a named group of steps. When the main program calls it, control jumps into the procedure, runs it, then returns to the line after the call. Step through.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"func","eyebrow":"Functions","title":"A routine that returns a value","body":"A function sends a value back. area(4, 5) receives w = 4 and h = 5, returns 20, and the main program stores it. Change the values.","controls":[{"id":"w","label":"w","min":1,"max":10,"step":1,"value":4},{"id":"h","label":"h","min":1,"max":10,"step":1,"value":5}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"mode":"reuse","eyebrow":"Why use routines?","title":"Write once, use many times","body":"Drawing three squares without a routine repeats the same code three times. With a routine it is written once and called three times."},{"mode":"quiz","eyebrow":"Check yourself","title":"Where next?","body":"A program calls a procedure on line 8. The procedure finishes.","question":"Which line runs next?","options":["Line 1","Line 9","Line 8 again"],"answer":1,"right":"Correct. Control returns to the line after the call.","wrong":"Not quite. Where did the call happen?","look":"call"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"A procedure runs as soon as it is written.\"","body":"It only runs when it is called."},{"tag":"Popular idea","title":"\"Procedures and functions are the same.\"","body":"A function returns a value; a procedure just does something."},{"tag":"Popular idea","title":"\"Parameters are global variables.\"","body":"Parameters receive values for that call only."}]},{"type":"cards","id":"code","eyebrow":"Pseudocode","title":"Routines","items":[{"tag":"Procedure","title":"Does a task","body":"PROCEDURE stars(n)\n    FOR i ← 1 TO n\n        OUTPUT \"*\"\n    NEXT i\nENDPROCEDURE\nCALL stars(3)","code":true},{"tag":"Function","title":"Returns a value","body":"FUNCTION double(x)\n    RETURN x × 2\nENDFUNCTION\ny ← double(7)   // y = 14","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Dance routine","items":[{"title":"Routines in a dance","time":"15 minutes","need":["Space to move","Paper"],"steps":["Invent a short 4-move routine and name it, e.g. 'Spin'.","Write a dance: Spin, clap twice, Spin, jump, Spin.","Perform it: each 'Spin' calls the routine.","Change the routine once. Does every Spin change?"],"why":"Changing a routine in one place changes every call, which is why programmers use them.","safety":"Clear a safe space."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"When does a procedure run?","options":["When the program starts","When it is called","Never"],"answer":1,"right":"Correct. Writing it only defines it.","wrong":"Not quite. Defined vs called."},{"question":"What does a function do that a procedure doesn't?","options":["Returns a value","Uses a loop","Has a name"],"answer":0,"right":"Correct. RETURN sends a value back.","wrong":"Not quite. Think of RETURN."},{"question":"FUNCTION add(a, b) RETURN a + b. What is add(3, 4)?","options":["34","7","12"],"answer":1,"right":"Correct. 3 + 4.","wrong":"Not quite. Substitute the arguments."},{"question":"What are a and b in add(a, b)?","options":["Outputs","Loops","Parameters"],"answer":2,"right":"Correct. They receive the values.","wrong":"Not quite. The values passed in."},{"question":"Why use a routine for code used 5 times?","options":["It runs slower","It hides errors","Write it once, easier to fix"],"answer":2,"right":"Correct. One place to change.","wrong":"Not quite. Think of maintenance."},{"question":"After RETURN, control goes…","options":["back to where the function was called","to the end of the program","to the first line"],"answer":0,"right":"Correct. The result is used there.","wrong":"Not quite. Where was it called from?"}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["what a procedure and a function are","how control jumps into a routine and returns","what parameters and return values are","why routines make programs shorter and easier to fix"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"routine","meaning":"A named block of code: a procedure or function."},{"word":"procedure","meaning":"A routine that performs a task."},{"word":"function","meaning":"A routine that returns a value."},{"word":"parameter","meaning":"A variable that receives a value when a routine is called."},{"word":"call","meaning":"Run a routine by using its name."}]},{"type":"sources","id":"sources","title":"Sources","items":["BBC Bitesize KS3 Computer Science","Raspberry Pi Foundation, Computing curriculum resources"]}]},"overlay":null}