functions Flashcards
a group of statements that exist within a program for the purpose of performing a specific task is a …….
function
a design technique that helps to reduce the duplication of code within a program is ……..
code reuse
the first line of a function is known as the ………
header
you ……. a function to execute it
call
a design technique that programmers use to break down an algorithm into functions is known as …….
top-down design
a ……. is a diagram that gives a visual representation of the relationships between functions in a program
hierarchy chart
a ……… is a variable that is created inside a function
local variable
a ……… is the part of a program in which a variable may be accessed
scope
an ………. is a piece of data that is sent into a function
argument
a ………… is a special variable that receives a piece of data when a function is called
parameter
a variable that is visible to every function in a program file is a ……..
global variable
when possible you should avoid using …….. variables in a program
global
library function
this is a pre-written function that is built into a programming language
‘random()’
this standard library function returns a random integer within a specified range of values
and/or
this standard library function returns a random floating-point number in the range of 0.0 up to but not including 1.0
‘uniform()’
this standard library function returns a random floating-point number within a specified range of values
‘return’
this statement causes a function to end and sends a value back to the part of the program that called the function
datagram chart
this is a design tool that describes the input, processing and output of a function
Boolean
this type of function returns true or false
differentiate
this is a maths module function
How do functions help you to reuse code in a program?
They let you perform the same operations over and over without having to retype every line of the implementation.
Name and describe the two parts of a function definition.
The header is the first line of a function definition and it defines the name and any parameter variables. The body defines what the function does.
When a function is executing, what happens when the end of the function block is reached?
At the end of the function block, the program goes back to the place where the function was called and continues executing statements from that point. Using the standard nomenclature, we say that control returns to the point that the function was called.
What is a local variable? What statements are able to access a local variable?
A local variable is a variable that is defined inside a function block without the global keyword. The only statements that are able to access a local variable are those that come after it within the same function block.
What is a local variable’s scope?
everywhere below the point the variable is defined that is within the same function as the variable
Why do global variables make a program difficult to debug?
Global variables make programs difficult to debug because when you find that you’re getting the wrong value in one, it’s hard to narrow down where the error is coming from.
What library function would you use to select a random number from;
0, 5, 10, 15, 20, 25, 30 ?
you would use the 'randrange()' function from the 'random' module specifically 'random.randrange(0, 31, 5)'
What statement do you have in a value-returning function?
The difference between a void function and a value-returning function is the existence of a ‘return’ statement in a value-returning function. This statement sends some value back to the point in the program where the function was called.
What three things are listed on an IPO chart?
Input: a brief description of the parameters of the function
Processing: a brief description of what the function is supposed to do
Output: a brief description of what value(s) the function returns
Note that the input and/or output columns may be empty, depending on what you want the function to do.
What is a Boolean function?
one that returns either ‘True’ or ‘False’
What are the advantages of breaking a large program into modules?
- If you’ve defined a bunch of functions, then by putting those in separate module files you won’t have to scroll past them to see the mainline logic of your program.
- Splitting a program into modules where each module is a collection of related functions makes it easier to reuse code between programs. You may find that you have several programs that all need to perform the same astronomy calculations (you’re an astronomer in this example), so the module you’ve created with a bunch of astronomy functions will be useful to import into each of them.
- Once you’ve used a module for a while, you can be pretty sure you’ll have worked out all the bugs in it. So it reduces the possibility of bugs.
- When working in a group, it’s both easier and more useful for each coder to work on a single module of related functions than on some random selection of functions/ sections of code.
What is a function?
A function is a group of statements that exist within a program for the purpose of performing a specific task.
What is an argument?
any piece of data that is passed into a function when the function is called
What is a parameter?
a variable that receives an argument that is passed into a function