Intro to Functions Flashcards
Function
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.
Invoking a function
“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!)
Function definition, defining a function
How a function gets defined before it gets invoked
What are Functions?
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
Single Responsibility Principle on Functions
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.
Invoking a function
“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!)
Argument
An argument is a piece of data delivered to a function when it’s being invoked.
Return value
A return value is the piece of data that a function delivers to the code that invoked the function.
What should we understand when we use a function?
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
len
Based on the one given value, will calculate the length of the given value. The given value is typically either a string or list.
Based on one given value, will display the given value to the “standard output” (like the console, terminal, bash, etc) as best it can.
random.randint
Based on two given values, generate a random integer in the range between the first given value and the second given value
time.time
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)
Arguments are Positional
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.
Passing in the Wrong Number of Arguments
If we pass in the wrong number of arguments, we get errors, like this TypeError
Function definition, defining a function
How a function gets defined before it gets invoked
Function Signature
A piece of syntax. A part of the function definition that determines function name and the parameter list
Parameter
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.
Guidelines for Creating a Function
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?
Describe each piece of code in the function signature from left-to-right:
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!
Return Statements End a Function Call
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.
Variable
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.
Variable Declaration
The line of code where we first introduce a variable, usually the first place where we give a variable a name and a value
Scope
The area of a program where a given variable can be accessed or used
Local Variable
A kind of variable that has the smallest scope: local scope. The most common kind of variable.
Global Variable
A kind of variable that has the largest scope: global scope.