Functions and Libraries
View Code
A lot of these definitions a notes are very interrelated. It is recommended that
you read this twice and see the Tracing
Functions page.
Functions
- Function
- a define block of coding able to be used in other blocks of code
- This also goes under possible names of method or sub method.
- Technically a function is suppose to return a value while a method
just performs an action.
- Function Header
- the name of a function plus other modifiers such as return type and
parameters
- Call
- to use a function in coding
- This is also known as invoking.
- Return
- a function returns a value
- A bit redundant, well think of this. If a function returns an integer,
a call to that function can be treated as a constant integer. So the
function can be used in things like assignment.
- void
- a function return type that specifies that there is no return value
- Overload
- to define a function with the same name but with different parameters
- Default
- to assign a value in a function parameter if no actual parameter is
passed
- Main
- a function often used to run the entire program
- Yes you have been writing functions all along. Main is a function that
is the program.
Variable Sight and Life
- Scope
- the range in which a variable can be accessed by a function
- Keep in mind each programming language has their own idea of scope.
- Life
- the range in which a variable exists while a program is running
- Global scope
- to be able to be accessed by all functions
- Global Variable
- a variable with global scope that exists from the beginning till the end
of a program
- Often these are declared outside of all functions.
- Local Variable
- a variable with scope of only the function they are defined in and only
exists from when they are declared till the end of the function they are
declared in
- Static Variable
- a variable that exist from its declaration till the end of a program
- You don't use or see these variable often in basic computer science,
unless they are important in a language e.g. Java.
Parameters
- Parameters
- variables that a function receives for processing
- There are a lot of terms for describe parameters.
- There are two types of parameters.
- There are four ways to pass variables are parameters.
- Formal Parameter
- the variable of the parameter of a function
- Actual Parameter
- the variable used to call the function
- This is also known as an argument.
- Pass
- to transfer information of the actual parameter to the formal parameter
- Pass by Value
- a parameter passes the value of the actual parameter
- In this type of passing a whole new variable is declared in a function
with the same value as the original.
- This is useful if you want the original variable unchanged and the
variable uses little memory e.g. int
- Pass by Reference
- a parameter passes the memory address/location of the actual parameter
- In this type of passing a reference to the original variable is
passed.
- This this means that whatever is done to the variable inside a
function also happens to the actual variable outside the function.
- Constant Pass by Reference
- a parameter that is passing by reference but is made constant
- Here a reference is passed but the variable inside the function is
constant (unchangeable).
- Why not pass by value? Since some variable take up a lot of memory
space e.g. objects it is better to not create a whole new
variable a use memory. In the end the program runs faster.
- Pass by Text
- a parameter passes the text of a variable
- Here text is passed...think of assigning a block of code to a
variable, so every time that variable occurs in the function you copy
and paste that block of code.
- This type of passing is uncommon and exists in older programming
languages. Only useful in trivia.
Specification Documentation
- Precondition
- a condition that must be true for a function to be called properly
- This is often a comment under or over a function header.
- Postcondition
- a condition that exist after the function is called
- This either tells what a function returns or what it changes
e.g. message printed to the screen
- This is also a programming comment with the precondition.
- Unlike the precondition there always must be a postcondition.
Libraries
- Library
- a file or group of files with defined functions that a program can
include so its function can be used
- Other various forms of libraries are packages and modules (but they
are programming language specific).
- This is also known as a header file.
- Often there is a basic set of libraries included in every language,
e.g. iostream for C++.
- Current
- the folder/directory where the code source file, programming project
file, executable file, or byte code file is
- Include
- the folder/directory where basic/default library files are
See also Tracing Functions.
Prev --
Back to Portal --
Next