Functions in C++
How to make sure you do not include files more than once by accident when there are separate files
Guard every header file
Given vector<int> ageVector put into a void function what will our function's parameters look like if ageVector consists of more than 10 elements and we are planing to modify the vector
void (vector<int>& ageVector) {
What is the syntax for accepting C-strings as function parameters?
void SpaceToHyphen(char* modString) { // this is the pointer way or void SpaceToHyphen(char modString[]) { // this way works too
When can you use default parameters with overloaded functions?
when the compiler can determine which function declaration to enter ex. int printDate(int x, int y, int z) { vs. int printDate(int x, int y, int z = 0) { int main () { printDate(4,1784); } // prints the default function
How to use a function in main { that was defined under main { } ?
you can write the function declarations above main ( )
what is the scope of a function?
Declaration to the bottom of the file or Definition to the bottom of the file(if no declaration)
What is the difference between function declaration and function definition syntax?
Declaration: int func(); definition: int func() { ... }
What is function overloading?
Declaring multiple functions with identical names
What happens if you redeclare variables within the same scope? ex. int cars = 7; int cars = 9;
Error fix: int cars = 7; cars = 9;
What does a reference variable do E.x. If Int usrValInt; Int& usrValRef = usrValInt;
Every time one variable in the code changes, the other changes too
Guard header file syntax
#ifndef HEADERF_H #define HEADERF_H ... // header file declarations code #endif
How many times can a function return?
1
How to function test our function HrToMin();
1. Create a seperate file 2. In main output "testing started" and "testing completed" 3. assert(HrToMin(0,0) == 0); I.e sample arguments and the expended value of the argument
When do we use PBR in a parameter?
1. When we are planning to modify the actual argument put into the parameter 2. When we are planning to accept large string or vectors as an argument (typically more than 10 objects)
When to use Global Variables and where do declare them?
It's not best practice to use them but if need be use them for const variables declared outside any { } scope a the top and can be used by any { }
What Must a function's parameters contain?
Parameters contain ONLY variable declarations Cannot contain expressions e.g. int myFunct(int user + 5) { // this doesn't work
What happens if assert macro fails?
Program never reaches "testing completed" and tells you which arguments the function fails on
what is the return type of any function?
The data type of the function ex. int myFunct() will return an integer
What is a parameter?
The declared variable types in a function's declaration Parameters do not have values until function is called
what is the syntax to declare a Pass-by-reference and what function type is this typically seen in?
Typically seen in void type void functName(type& varName) { // this will essentially modify the variable that was sent in to the function as an argument instead of modifying a copy
When can you overload functions?
When the functions are differing in the number of parameters or types of parameters the return types differing but having identical parameter types and quantity will still throw an error
what does C-strings look like when passed as C-string function arguments
char myArray[]; StrSpacetoHypen(myArray); // just the name no [] or *
What does header files consist of?
declarations
What does .cpp (not main.cpp) consist of
definitions (of functions mostly)
what does the #include "..." or #include "__ .h" direct the compiler to do and when is this used over #include <...>
directs compiler to look for file in the same directory typically always for user defined stuff .h tells to include header file
How to write a proper function stub (a reminder to complete an incomplete function)
in the body, put a "FIXME: functName" << endl; then return an arbitrary type of the function e.g. double CalcMpg(double email, double cormail) { cout << "FIXME: CalcMpg" << endl; return 0.0;
Can you overload functions with different return types but identical parameter quantities and types?
no ex. double AgeAvg(int x) { return x + 4.4; } int AgeAvg(int y) { return y + 2; } yields an error
What is the preprocessor directive?
preprogram scan looking for #
Given vector<int> ageVector put into a void function what will our function's parameters look like if ageVector consists of less than 10 elements and we are planning to modify the vector
void (vector<int>& ageVector) {
What does #include directive tell the compiler to do?
replace that line by the contents of the included file
what does the return of void implicitly look like?
return;
what does #include <...> directive tell the compiler to do?
tells the compiler to check the system standard library directives and replace that line by the contents of the included file
How to declare a function that has some parameters that are default while some others are required parameters?
the default parameters go to the very right of the function( ) while the non default parameters go to the very left ex. void CalcStal(int num1, int num2, char Usr = 'a', int num3 = 0) {
what to include in the main.cpp file to use functions declared in other files?
the header file
Given vector<int> ageVector put into a void function what will our function's parameters look like if ageVector consists of less than 10 elements and we are not planning to change it at all?
void (const vector<int> ageVector) {
Given vector<int> ageVector put into a void function what will our function's parameters look like if ageVector consists of more than 10 elements and we are not planning to change it at all?
void (const vector<int>& ageVector) {