User Defined Functions Flashcards
What is a subprogram?
Groups of code that break large programs down into smaller parts. In processing, these are called functions (methods in Java and C#)
What do functions allow programmers to do?
- write a large program piece by piece
- many programmers can work on separate parts of the program
- standard libraries of subprograms can be used by many people
- functions with parameters allow variation in the function outcome
What are functions?
Blocks of code which encapsulate some task into a black box or module
What things make up all functions?
- a name
- some data items they accept, with specific types (parameters)
- a type of data item that they give back as a result (or void)
What is the signature of the function?
The name and the types
eg. float getPercent(int, int)
What happens of values passed in as parameters?
They are copied and stored in the function’s local variable.
What happens if the you change the value of the local variable in the function?
The values passed will not change
What happens to local variables after a function is finished executing?
They disappear
How many return types can a function have?
one or none
What happens when the return statement is executed?
The function is exited and optionally returns the value from it.
Are return statements required?
If the return type is not void, then yes. Otherwise, they are optional. It is best practice to only have one return statement
What happens when you pass a String into a function?
The address stored in the original String variable is copied into the local variable. This means that any changes made to the String will be reflected outside of the function
What is the header?
The first line of code of an actual function definition. Unlike the signature, which is an abstraction of the header and is only used to match function calls to functions
What is function overloading?
Defining multiple functions with the same name but different parameter lists (two functions with the same name must have different signatures)
Can you use variables of the same name outside of a function?
Yes. Parameters and variables defined in a function become local to that function