Function
When called, memory is allocated to the stack for its value parameters, local variables, and name (if not void). It calls statements until a return statement or at closing brace, then returns control to where it was called.
What must happen before a function is called?
The compiler must process either the function protype or the function definition.
Function Declaration
Identifier that names a function and specifies the number of parameters, type of each parameter, and type of return value.
What is the basic syntax for a function declaration?
Return_type function_name(parameter declarations);
(also called the function prototype).
What is the basic syntax for a function defenition?
Return_type function_name(parameter declarations) {
statements
}
Formal Parameter
Dummy variable listed in the
subprogram header and used in the subprogram.
Actual Parameter
Represents a value or address used in the subprogram call statement.
Pass-By-Value
Pass a copy of the parameter value.
Pass-By-Reference
Pass a reference to the parameter value (using a pointer or address).
Default Parameters
Simple types, structs, and classes are value parameters by default but can be made reference parameters by using ‘&’ after their type. Arrays are always reference parameters.
Reference Parameters
Used when the function is to assign or change the value of a variable from the calling block.
Function Overloading
Functions with the same name but different number/type of parameters.
Array
A group of elements of the same type in contiguous memory locations.
Array Indexing
Accessing member x[i] requires calculation from x[0], the first element in the array.
Characteristics of C/C++ Arrays
Do not behave like first-class objects; they cannot be copied using ‘=’. They do not remember how many items it can store and do not check for index validity.
Array Indexing in C/C++
Creating an array creates a pointer to the first element in the array. For example, int x[10] creates an integer array with 10 elements and assigns x a constant pointer type (int*) with value &x[0].
C-Style String
An array of characters ending with the null character ‘\0’. Can convert from C++ string to C-style using c_str().
Pointer
A variable that contains a memory address. They can be used to access memory stored in the heap. Pointers must be explicitly declared and they must be initialized.
Null Pointer
A special pointer value that does not refer to any valid memory location. It is given the ‘nullptr’ keyword.
Pointer Assignment
One of two fundamental pointer operations. Used to set a pointer variable’s value to some useful
address. The ‘*’ operator is used for struct/class names while the ‘->’ operator is used for struct/class variables.
Pointer Dereferencing
One of two fundamental pointer operations. Yields the value stored at the location represented by
the pointer’s value. Can be explicit or implicit.
Dereferencing an Uninitialized/Null Pointer
Causes undefined behavior. There could be a crash, a core dump, or a segmentation fault.
C/C++ Valid Pointer Operators
The operators ‘=’, ‘*’, and ‘->’ for pointers and the relational operators ‘==’ and ‘!=’.
C/C++ Pointer Arithmetic
Pointers can be added to or subtracted from. Take the lines:
float stuff[100]
float *p;
p = stuff;
for example. *(p + 5) would point to stuff[5].