{"base":{"id":"cs11-02","subject":"Programming","title":"Parameters, local and global variables","scene":"mix","steps":[{"use":"cs11","mode":"params","eyebrow":"Parameters","title":"Passing values in","body":"Parameters are placeholders in a subroutine; arguments are the actual values passed. Change the arguments.","controls":[{"id":"w","label":"width argument","min":1,"max":10,"step":1,"value":4},{"id":"h","label":"height argument","min":1,"max":10,"step":1,"value":3}]},{"use":"cs11","mode":"scope","eyebrow":"Scope","title":"Local and global","body":"Local variables exist only inside a subroutine; global variables can be used anywhere. Step through.","actions":[{"id":"step","label":"Step"},{"id":"reset","label":"Reset"}]},{"use":"code9","mode":"call","eyebrow":"Tracing calls","title":"Jump in and back","body":"Trace a program that calls a subroutine with parameters.","actions":[{"id":"step","label":"Step"},{"id":"run","label":"Run"},{"id":"reset","label":"Reset"}]},{"use":"cs11","mode":"quiz","look":"scope","eyebrow":"Check yourself","title":"Scope","body":"A variable is declared inside a procedure.","question":"Where can it be used?","options":["Anywhere in the program","Only inside that procedure","Only in the main program"],"answer":1,"right":"Correct.","wrong":"Not quite."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","items":[{"tag":"Parameters","title":"Pseudocode","body":"PROCEDURE Stars(n : INTEGER)\n    FOR i ← 1 TO n\n        OUTPUT \"*\"\n    NEXT i\nENDPROCEDURE\nCALL Stars(5)","code":true},{"tag":"Local","title":"Python","body":"def double(x):\n    result = x * 2   # local\n    return result","code":true},{"tag":"Global","title":"Python","body":"score = 0\ndef add_point():\n    global score\n    score += 1","code":true},{"tag":"Trace","title":"Arguments","body":"Area(3, 4) → width = 3, height = 4 → returns 12","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Overusing global variables","body":"They can be changed anywhere, making bugs hard to find."},{"tag":"Mistake","title":"Mixing up parameters and arguments","body":"Parameters are in the definition; arguments are in the call."},{"tag":"Mistake","title":"Using a local variable outside its subroutine","body":"It no longer exists."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Good practice","title":"Pure functions","body":"A function that uses only its parameters and returns a value, changing nothing else, is easy to test."},{"tag":"Languages","title":"By value and by reference","body":"Some languages pass a copy (by value); others pass the original (by reference), so changes inside affect the caller."}]},{"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 Max2(a, b) that returns the larger value.\n\nAnswer:\nFUNCTION Max2(a, b) RETURNS INTEGER\n    IF a > b THEN RETURN a ELSE RETURN b ENDIF\nENDFUNCTION","code":true},{"tag":"Challenge","title":"Explain","body":"Why might two procedures both use a local variable called i without problems?\n\nAnswer: each i is local to its own procedure; they are separate variables."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Try it yourself","items":[{"title":"Scope experiment","time":"15 minutes","need":["Python"],"steps":["Create a variable inside a function and try to print it outside.","Read the error message.","Fix it by returning the value."],"why":"You see local scope in action.","safety":"None."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"A parameter is…","options":["the value passed in","a global variable","a variable in a subroutine definition"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"An argument is…","options":["the value passed when calling","the subroutine name","a loop counter"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"Local variables exist…","options":["everywhere","only inside their subroutine","after the program ends"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"Global variables can be used…","options":["only in one procedure","never","anywhere in the program"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"Why prefer local variables?","options":["Fewer unexpected side effects","They are faster to type","They are always shorter"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"In Python, to change a global inside a function you write…","options":["local score","global score","return score"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"Area(5, 2) returns 10. The arguments are…","options":["width and height","10","5 and 2"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"After a function ends its local variables…","options":["are removed","become global","are printed"],"answer":0,"right":"Correct.","wrong":"Not quite."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["define parameters and arguments","write subroutines with parameters","explain local and global scope","explain why local variables are good practice"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"Parameter","meaning":"Placeholder for a value passed into a subroutine."},{"word":"Argument","meaning":"The actual value passed."},{"word":"Local variable","meaning":"Exists only within a subroutine."},{"word":"Global variable","meaning":"Can be used anywhere."},{"word":"Scope","meaning":"Where a variable can be used."}]},{"type":"sources","id":"sources","title":"Sources","items":["Python documentation: scopes and namespaces","Raspberry Pi Foundation, Isaac Computer Science: scope","BBC Bitesize, GCSE Computer Science: local and global variables","Khan Academy: functions with parameters"]}]},"overlay":null}