Intro to Functions Flashcards

1
Q

Function

A

Lines of code (1 or more) that are related, grouped together, and named. Once defined, these lines of code are reusable and can be called over and over again.

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

Invoking a function

A

“Invoking a function” means “make the lines of code inside of a function definition happen now.” We can invoke a function any number of times (even infinitely!)

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

Function definition, defining a function

A

How a function gets defined before it gets invoked

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

What are Functions?

A
Functions are lines of code that:
hold related, sequential logic
usually have a name
don't do anything, until we tell it to do something
take in data
return a result
are reusable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Single Responsibility Principle on Functions

A

Single Responsibility Principle is a programming value/best practice, or a belief that many/most developers value. The Single Responsibility Principle (SRP) states that a function should have one, and only one primary responsibility. This is to say, when developers describe the purpose of a function, ideally, the function isn’t trying to do more than one thing.

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

Invoking a function

A

“Invoking a function” means “make the lines of code inside of a function definition happen now.” We can invoke a function any number of times (even infinitely!)

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

Argument

A

An argument is a piece of data delivered to a function when it’s being invoked.

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

Return value

A

A return value is the piece of data that a function delivers to the code that invoked the function.

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

What should we understand when we use a function?

A

We should fully understand the responsibility of each function when we use it. Before using a function, we should be able to describe the following:

the purpose of the function
the data that 'goes into' the function
the computations inside of the function
the data that 'goes out' of the function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

len

A

Based on the one given value, will calculate the length of the given value. The given value is typically either a string or list.

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

print

A

Based on one given value, will display the given value to the “standard output” (like the console, terminal, bash, etc) as best it can.

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

random.randint

A

Based on two given values, generate a random integer in the range between the first given value and the second given value

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

time.time

A

Calculates the number of seconds passed since epoch. For Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins)

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

Arguments are Positional

A

The arguments that we pass in to a function have a specific order, and the order matters.

The correct order of arguments is determined by whoever defined the function.
An incorrect order of arguments in a function is misusing the function, and will result in bugs/unexpected output.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Passing in the Wrong Number of Arguments

A

If we pass in the wrong number of arguments, we get errors, like this TypeError

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

Function definition, defining a function

A

How a function gets defined before it gets invoked

17
Q

Function Signature

A

A piece of syntax. A part of the function definition that determines function name and the parameter list

18
Q

Parameter

A

The name of an expected argument for this function. This parameter name is the name of the local variable that will hold an argument value. Parameters and arguments at the time of function call are mapped position-ally.

19
Q

Guidelines for Creating a Function

A

Before we create a function, we should do our best to determine:

  • *What is the kind of logic you want to put into a function?
  • *What is this function responsible for doing?
  • *What is the best name for this function?
  • *Ideally, the name of the function implies the responsibility of this function.
  • *What kind of arguments should the function take in?
  • *How many arguments?
  • *What one piece of data should the function return?
  • *What is its data type, and what does it represent?
20
Q

Describe each piece of code in the function signature from left-to-right:

A

def def is a special keyword in Python. Python interpreters read def and go “Hey! I’m beginning to define a function!”, and reads the rest of the line as a function definition.

function_name Replace this part with the name of the function you are defining.

( … ) These round parentheses contain the parameter list. Ensure that it’s inbetween the function name and colon( : )! Ensure that there is an ending ) !

apples, oranges This is the function’s parameter list. Replace this! Arguments named inside of a function signature are technincally called parameters. Here is a comma-separated list of the parameters of this function. There can be 0 parameters, in which case the parens would be empty, and the function signature would look like def function_name():

: This colon ends the function signature, and begins the function body. It’s easy to not remember this colon!

21
Q

Return Statements End a Function Call

A

Return statements always end and exit a function call. When a program reaches a return statement, it will immediately take the return value, and return back to the line of code that invoked the function.

22
Q

Variable

A

A variable is a name that references a value, or piece of data, stored in computer memory. We can assign values to variables, and read values from variables.

23
Q

Variable Declaration

A

The line of code where we first introduce a variable, usually the first place where we give a variable a name and a value

24
Q

Scope

A

The area of a program where a given variable can be accessed or used

25
Q

Local Variable

A

A kind of variable that has the smallest scope: local scope. The most common kind of variable.

26
Q

Global Variable

A

A kind of variable that has the largest scope: global scope.