Module 4 - Functions Flashcards

1
Q

What is a function?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the ‘divide and conquer’ approach?

A

The problem is divided into smaller sub-problems and then each problem is solved independently

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is modularized programming?

A

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!)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a void function?

A

Simply executes the statements it contains then terminates or stops, without returning a value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a value-returning function?

A

Executes the statements it contains, and then it returns a value back to the statement that called it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules for naming functions?

A
  • can only contain numbers, letters and underscores
  • cannot start with a number
  • cannot use keywords
  • case sensitive!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define function syntax

A

def function_name():
____statement1
____statement2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the different parts of a function?

A

Header = keyword and function name, followed by paranthesis and a colon
Body (called blocks) = indented statements included in the function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the three important things to consider when defining a function?

A
  1. Major goal of the function (this will define the statements inside the function)
  2. What input data does it need (this will specify the argument list)
  3. What value does it need to give back to the caller (this is the return value)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a function to determine the larger of two numbers and print it. Then call that function with numbers 7 and 3.

A
def large_num(num1,num2):
\_\_\_if num1>num2:
\_\_\_\_\_\_\_print(num1)
\_\_\_else:
\_\_\_\_\_\_\_print(num2)

large_num(7,3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the main function?

A

A function that activates (calls) other functions when they are needed
Main function is called when the program starts

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does a function look like in a flowchart?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is top down design?

A

A technique for breaking algorithm into functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a hierarchy (structure) chart do?

A

Depicts relationship between functions

Boxes represent functions; lines connecting boxes represent the functions called by each function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the math module?

A

Part of standard library that contains functions that are useful for performing mathematical calculations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you access the math module?

A

Use import statement “import math”

17
Q

How do you access the math functions?

A

To access one of the functions, you have to specify the name of the module and the name of the function, separated by a dot (also known as a period). This format is called dot notation:

math. sin(x)
math. pi

18
Q

Does the argument of a function have to be a literal?

A

No, the argument of a function can be any kind of expression, including arithmetic operators:
x = math.sin(degrees / 360.0 * 2 * math.pi)
And even function calls:
x = math.exp(math.log(x+1))
Almost anywhere you can put a value, you can put an arbitrary expression

19
Q

How do you end a function?

A

Enter an empty line

20
Q

What do you have to keep in mind with flow of execution and functions?

A

You must define a function before it is called

21
Q

What are parameters?

A

Variables that arguments get assigned to
When you define a function, the parameter is what’s in the parentheses

Then, when you call it, you enter in your arguments

22
Q

T/F: when you create a variable inside a function, it is local

A

True

23
Q

Are parameters local or global variables?

A

Local

24
Q

Is print_twice a void or value-returning function?

A

Void because it simply performs the task (print the argument twice) without returning a value

25
Q

How can you tell if a function is void or value-returning?

A

If you assign the result to a variable, then print(variable) the output will be a value called ‘none’

26
Q

Is the value ‘none’ the same as the string ‘none’?

A

No, it is a special value with its own type ‘NoneType’

27
Q

Write the code to call a function named send_variable and that expects a single int parameter.
Suppose a variable called x refers to an int. Pass this variable as an argument to send_variable.

A

send_variable(x)

28
Q

What is the syntax of a statement that calls a function?

A

function_name()

*MUST INCLUDE parentheses even if no arguments!!!

29
Q

Given that add, a function that expects two int parameters and returns their sum, and given that two variables, euro_sales and asia_sales, have already been defined:
Write a statement that calls add to compute the sum of euro_sales and asia_sales and that associates this value with a variable named eurasia_sales.

A

eurasia_sales=add(euro_sales,asia_sales)