Chapter 56 - Subroutines and recursion Flashcards
What is a subroutine?
» A name block of code which performs a specific task within a program
What are the 2 types of subroutines?
» Functions
» Procedures
What is the difference between a function and a procedure?
» The function returns a value but the procedure doesn’t
» if there are values already in the subroutine, you use a procedure
How can you call the subroutine from the main program and output it to the user?
» Simply write the subroutine name
What does the first line of a subroutine specify?
» The interface for that subroutine
What is modularity?
» Concept of breaking a large program or problem down into smaller chuncks
What is the benefit of modularity?
» Each module carries out a single specific task
What does a procedure do?
» Performs a set task and can take in a zero, 1 or 2 inputs
What happens when a parameter is passed by a value?
» The value created for the subroutine being called is a copy of the original
» Held in a separate memory location once passed
» It is now a local variable
What happens if you change the parameter inside the subroutine when you pass a value?
» It will not affect its value outside the subroutine
What does it mean if a parameter is passed by a reference?
» The address of the parameter is passed to the subroutine
» A pointer that contains the memory address of the original variable is created
What happens if you change the value in the parameter when passing by a reference?
» A change to this value within the called subroutine will also affect the value outside the subroutine
» It becomes a global variable
What does it mean if you say a scope of a variable?
» Refers to where a variable is visible or accessible from inside the main program
What is a global variable?
» A variable which is defined in the main program, outside any functions
» Has a global scope
What are some disadvantages of using a global variable?
» Needs to be preserved in memory all the time, this means memory space is kept throughout the running of the program
» Makes it less memory efficient
Why do we avoid using global variables?
» Increases complexity of the program - as they can be used by many difference parts of the program
» Make them harder to keep track of its value
» If you want to re-use code, you have to track and remember the value of the global variable
Why do we used global variables?
» To use a variable which can be accessed from any parts of a program
» When a value needs to stay the same throughout the program
What is a local variable?
» A variable which is defined in the procedure or function, and can be used only inside that function
What are the advantages of using a local variable?
» Local variables in difference subroutines can have the same identifier
» Changing the value of a local variable in a subroutine, will not affect its value outside the subroutine
What are the disadvantages of using a local variable?
» Can overwrite global variables with the same name
» Only exists during the execution of the program
What the principle of encapsulation?
» The subroutine written according to this principle can be tested independently, and used in many different programs without the programmer needing to know what variable it uses
What are the benefits of programming with subroutines?
» Relatively easy to understand and debug
» Can be tested independently
» It can be re-used
» Can use a modular approach
When is a subroutine recursive?
» If its defined in terms of tiself
What is recursion?
» When a subroutine calls itself from within its own subroutine
What is sequence?
» Executing the instructions one after the other
What is selection?
» A programming construct that allows a program to change direction depending on the outcome of a condition
What is iteration?
» Programming construct which enables us to repeat sections of code
What are 2 types of loops?
» Count controlled loops
» Condition controlled loop
What are the 3 characteristics a recursive subroutine should have?
» Contain a stopping condition
» For any input value other than the stopping condition, the subroutine should call itself
» Stopping condition should be reachable within a finite number of times
What are the problems if you don’t have the right characteristics of a recursive subroutine?
» May result in a stack overflow
What is a programming paradigm?
» A style of coding
How can we identify a global variable?
» Defined at the start of a program
» Exits throughout the prgoram
» Allows data to be shared throughout modules
Why use local variables?
» Prevents variables being accidentally being overwritten
Which variable is more memory efficient?
» Local
Which variable is used when a value doesn’t tend to change throughout the run time of a program?
» Global
Which variable is accessible throughout a program?
» Global
Which variable increases complexity?
» Global
Which variable allows data to be lost once a subroutine is no longer being used?
» Local
Which variable is retained in memory permanently?
» Global
What is the Recursive call?
» An instance of a subroutine calling itself
What is a base case?
» A condition in a recursive ubroutine that when true, returns control to the subroutine that called it rather than running antoher recursive instance
What are some advantages of recursion?
» Some problems are easier to think of recursively
» Recursive solutions can be more elegant
What are some advantages of recursion?
» Some problems are easier to think of recursively
» Recursive solutions can be more elegant
What are some disadvantages of recursion to iteration?
» It requires more memory in order to maintain the stack of calls
» If the space on the stack runs out, the program will run out
» Overheads of maintaing a stack means it will perform more slowly then iterative equivalent
What are all parameter’s passed by in python?
» By value
What is the difference between parameters and arguements?
» Parmaters are the variables whose values are taken in by a subroutine and the arguements are the values passed to them
Describe the use of local variables
» Local scope - only affect the variable inside the subroutine - reduces complexity of code when co sharing
» Store identifer with the same name
» Less memory used then global
State two features of global variables that distinguish them from local variables
» Can be accessed at any point in a program and are defined at the start of the program
» Continously held in memory, and the variable data does not get lose when the subroutine ends