Sub Programs Flashcards
Card 1 of 8
All 8 cards in this deck
What is the difference between a function and a procedure?
A function returns a value to the code that called it; a procedure carries out a task but returns no value.
How do you call an existing function and keep the value it returns?
e.g. use
calculatePrice()for 3 tickets and store the result intotalPricetotalPrice = calculatePrice(3)— the argument goes in the brackets and the return value is assigned with=(not==).What is the difference between a local and a global variable?
A local variable can only be used inside the subprogram where it is declared; a global variable is declared in the main program and can be used by all subprograms.
True or false? A variable declared inside a procedure can still be used in the main program after the procedure ends.
False. It is a local variable, so it only exists within that subprogram.
How do you pass an array into a sub program?
e.g. pass the arrayscoresinto a procedureprintAllUse the array's name as the argument in the call:
printAll(scores)
The parameter is then used like an array inside the sub program.Give two benefits of writing code as sub programs.
Any two, e.g.: the program is better structured; the code is easier to read; a block of code is written once and called whenever needed instead of being repeated; each sub program can be tested on its own; the code is easier to maintain.
What is the difference between a parameter and an argument?
e.g.procedure showArea(width)called withshowArea(8)A parameter is the variable in the sub program's definition that receives data (
width). An argument is the actual value passed in when it is called (8).In OCR Exam Reference Language, how do you declare a global variable?
e.g. a variablelivesstarting at 3Put the keyword
globalbefore the assignment:global lives = 3.