{"base":{"id":"cs11-01","subject":"Programming","title":"Procedures and functions","scene":"mix","steps":[{"use":"code9","mode":"call","eyebrow":"Subroutines","title":"Jump in, run, jump back","body":"A subroutine is a named block of code. Calling it runs the block, then returns to where it was called.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"use":"code9","mode":"func","eyebrow":"Functions return a value","title":"A routine that answers","body":"A function calculates and RETURNS a value; a procedure just does a job. Change the inputs.","controls":[{"id":"w","label":"Width","min":1,"max":10,"step":1,"value":4},{"id":"h","label":"Height","min":1,"max":10,"step":1,"value":5}],"actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"use":"code9","mode":"reuse","eyebrow":"Why use them?","title":"Write once, use many times","body":"Subroutines avoid repeated code, make programs easier to test and let teams split work."},{"use":"code9","mode":"quiz","look":"func","eyebrow":"Check yourself","title":"Procedure or function?","body":"A subroutine works out the VAT on a price and sends the result back.","question":"What is it?","options":["A procedure","A function","A loop"],"answer":1,"right":"Correct: it returns a value.","wrong":"Not quite."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","items":[{"tag":"Procedure","title":"Pseudocode","body":"PROCEDURE Greet(name : STRING)\n    OUTPUT \"Hello \" & name\nENDPROCEDURE\nCALL Greet(\"Ana\")","code":true},{"tag":"Function","title":"Pseudocode","body":"FUNCTION Square(n : INTEGER) RETURNS INTEGER\n    RETURN n * n\nENDFUNCTION\nx ← Square(7)","code":true},{"tag":"Python","title":"def","body":"def area(w, h):\n    return w * h\n\nprint(area(4, 5))","code":true},{"tag":"Use","title":"Menu","body":"CASE OF choice\n  1: CALL NewGame()\n  2: CALL ShowScores()\nENDCASE","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Forgetting RETURN in a function","body":"Without RETURN the caller gets nothing."},{"tag":"Mistake","title":"Printing instead of returning","body":"OUTPUT shows a value; RETURN sends it back for the program to use."},{"tag":"Mistake","title":"Defining but never calling","body":"A subroutine does nothing until it is called."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Industry","title":"Libraries of functions","body":"Large programs contain thousands of small, well-named functions. Good names make code read like a story."},{"tag":"History","title":"Subroutines","body":"Subroutines were invented in the late 1940s for early computers like EDSAC so that common code could be reused."}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one on paper before opening the card.","items":[{"tag":"Challenge","title":"Write","body":"Write a function IsEven(n) that returns TRUE if n is even.\n\nAnswer:\nFUNCTION IsEven(n : INTEGER) RETURNS BOOLEAN\n    RETURN n MOD 2 = 0\nENDFUNCTION","code":true},{"tag":"Challenge","title":"Trace","body":"def f(x): return x * 2 + 1\nprint(f(f(3)))\n\nAnswer: f(3) = 7, f(7) = 15."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Try it yourself","items":[{"title":"Refactor a program","time":"20 minutes","need":["Python"],"steps":["Write a program that prints a 3-line border three times.","Rewrite it with a procedure called border().","Compare the length of both versions."],"why":"Subroutines remove repetition.","safety":"None."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"A function…","options":["never takes inputs","cannot be reused","returns a value"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"A procedure…","options":["performs a task without returning a value","must return a value","is a variable"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"In Python, functions are defined with…","options":["func","def","proc"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"Calling a subroutine means…","options":["deleting it","naming it","running it"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"A benefit of subroutines is…","options":["less repeated code","slower programs","more variables"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"RETURN…","options":["prints to screen","sends a value back to the caller","ends the program"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"Square(4) with RETURN n * n gives…","options":["8","4","16"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"Subroutines help teams because…","options":["work can be split up","they hide errors","they need no testing"],"answer":0,"right":"Correct.","wrong":"Not quite."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["explain subroutines","tell procedures from functions","write and call procedures and functions in pseudocode and Python","explain the benefits of subroutines"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"Subroutine","meaning":"A named block of code that can be called."},{"word":"Procedure","meaning":"A subroutine that does not return a value."},{"word":"Function","meaning":"A subroutine that returns a value."},{"word":"Call","meaning":"Running a subroutine."},{"word":"Return value","meaning":"The value a function sends back."}]},{"type":"sources","id":"sources","title":"Sources","items":["Python documentation: defining functions","Raspberry Pi Foundation, Isaac Computer Science: subroutines","Computer History Museum: EDSAC and early subroutines","BBC Bitesize, GCSE Computer Science: subroutines"]}]},"overlay":null}