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();