3 - Function basics Flashcards
C++ comes with libraries of predefined functions that you can use in your programs. There are two kinds of functions in C++. Which?
- Functions that return a value
2. Functions that don’t return a value (void functions)
What is an “argument”?
The value a functions starts out with. E.g. :
sqrt (9.0);
Here, the double 9.0, is the argument.
What is the difference between an argument and a parameter?
The formal parameter is what’s given in the function declaration, the actual argument is what’s passed when calling the function.
What is this called? #include
It’s an include directive, which names the library cmath. It indicates that the code below will be using functions from that library.
There are include directives. Are there others?
There is also the using directive, which we use when we write:
using namespace std;
The abs() and labs() functions give absolute values. Which library are they in?
They’re in the cstdlib (C Standard General Utilities Library) library.
What do you use the cstdlib function abs for?
For producing the absolute value of a number type int.
What do you use the cstdlib function labs for?
For producing the absolute value of a number type long.
What do you use the cmath function fabs for?
For producing the absolute value of a number type double.
What must you include and use in order to write 2^3 = 8 in C++?
You need to use the cmath library, and use the function pow(x,y).
How do you round up numbers?
With the cmath function ceil(); Only works on doubles.
How do you round down numbers?
With the cmath function floor(); Only works on doubles.
How do you end the program from a place in your code?
With the cstdlib function exit(1); Only works on integers.
How do you produce a “semi-random” number?
With the cstdlib function rand();
How do you set the seed (starting value) for rand?
With the cstdlib function srand();
Are there any restrictions on the use of the pow( ) function?
Many implementations of pow have a restriction on what arguments can be used. In these implementations, if the first argument to pow is negative, then the second argument must be a whole number. It might be easiest and safest to use pow only when the first argument is positive.
What integer should you use as an argument to the exit ( ) function?
Any! But by convention, 1 is used for a call to exit that is caused by an error, and 0 is used in other cases.
All standard functions have argument types they’re meant for. What happens if you use the wrong type?
In many cases, some automatic type conversion will be done for you by C++. However, the results may not be what you intended.
What numbers will you have a chance of getting if you use the function rand ( ) ?
Any number in the range 0 to at least 32767. (32767 is the maximum two-byte positive integer).
How do you scale the rand ( ) function, i.e. to give you random numbers between 0 and 10?
You have to use:
rand ( ) % 11.
Why is the rand ( ) function not truly random?
A sequence of calls to the function rand will produce a sequence of numbers that appear to be random. However, if you could return the computer to the state it was in when the sequence of calls to rand began, you would get the same sequence of “random numbers”.
Define: local variable.
Variables that are declared within the body of a function definition are called local variables.
What is a function declaration? Give an example.
A function declaration is the part of the code that tells the compiler that a function exists. E.g: double TotalCost (int x, int y);
What is a good way to make a function declaration comment?
One good way to write a function declaration comment is to break it down into two kinds of information called the precondition and postcondition.