Module 4 - Functions Flashcards
What is a function?
A group of statements in a program that perform a specific task
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.
What is the ‘divide and conquer’ approach?
The problem is divided into smaller sub-problems and then each problem is solved independently
What is modularized programming?
Each task within the program is in its own function
(you have a dishwasher, washing machine, robot vacuum - if one breaks, the other machines won’t be disturbed = MODULARIZATION!)
What is a void function?
Simply executes the statements it contains then terminates or stops, without returning a value
What is a value-returning function?
Executes the statements it contains, and then it returns a value back to the statement that called it
What are the rules for naming functions?
- can only contain numbers, letters and underscores
- cannot start with a number
- cannot use keywords
- case sensitive!
Define function syntax
def function_name():
____statement1
____statement2
What are the different parts of a function?
Header = keyword and function name, followed by paranthesis and a colon
Body (called blocks) = indented statements included in the function
What are the three important things to consider when defining a function?
- Major goal of the function (this will define the statements inside the function)
- What input data does it need (this will specify the argument list)
- What value does it need to give back to the caller (this is the return value)
Write a function to determine the larger of two numbers and print it. Then call that function with numbers 7 and 3.
def large_num(num1,num2): \_\_\_if num1>num2: \_\_\_\_\_\_\_print(num1) \_\_\_else: \_\_\_\_\_\_\_print(num2)
large_num(7,3)
What is the main function?
A function that activates (calls) other functions when they are needed
Main function is called when the program starts
What does a function look like in a flowchart?
Function call shown as rectangle with vertical bars at each side
Function name is written inside the rectangle
End terminal symbol usually reads ‘return’
Typically draw a separate flowchart for each function
What is top down design?
A technique for breaking algorithm into functions
What does a hierarchy (structure) chart do?
Depicts relationship between functions
Boxes represent functions; lines connecting boxes represent the functions called by each function
What is the math module?
Part of standard library that contains functions that are useful for performing mathematical calculations