Functions Flashcards
Calling a function
Ex built-in function pow(x,y)
Pow = function name
X and y are arguments passed into pow
Return value
Naming functions
One or more characters long consisting of letters, numbers, or underscore character.
Parts of a function
Function header - begins with def to :
Def = definition, Name of function, functions parameter list, then :
Documentation string
Function body
Indented block of code
Return a value - then jumps out of the function to the point in the program where it was called. Return id usually the last line of a function to be executed.
Function definition
A reusable chunk of code
Block of code with a name that takes input, provides output, can be stored.
What is the “scope “ of a variable or function?
It is where in the program a variable or function is accessible or visible.
Local variable
Any variable assigned for the first time within a function
Local variables are usable only within the function they are local to.
Global variables
Variables declared outside of any function
They are readable anywhere by any function or code within the program.
Main() function
The main() function is by convention assumed to be the starting point of your program.
Function parameters
Parameters pass data into a function
Pass by reference
When you pass parameters, the function refers to the original passed values using new names.
Default parameter values
Default values are only evaluated once, the first time they are called.
Keyword parameters
To call a function that uses keyword parameters, pass data in the form param = value.
Benefits
Make parameter values clear and easier to read
The order in which you call keyword parameters does not matter.
Module
A module is a collection of related functions and variables.
A module is a toolbox of helpful functions that you can use when writing other programs.
Usually does not have a main() function.
Functions
A named piece of code. Can take any number and type of input parameters, and return any number and t ype of output results.
Define function
Call function