3 - Function basics Flashcards

1
Q

C++ comes with libraries of predefined functions that you can use in your programs. There are two kinds of functions in C++. Which?

A
  1. Functions that return a value

2. Functions that don’t return a value (void functions)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an “argument”?

A

The value a functions starts out with. E.g. :
sqrt (9.0);
Here, the double 9.0, is the argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between an argument and a parameter?

A

The formal parameter is what’s given in the function declaration, the actual argument is what’s passed when calling the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is this called?
#include
A

It’s an include directive, which names the library cmath. It indicates that the code below will be using functions from that library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

There are include directives. Are there others?

A

There is also the using directive, which we use when we write:
using namespace std;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The abs() and labs() functions give absolute values. Which library are they in?

A

They’re in the cstdlib (C Standard General Utilities Library) library.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What do you use the cstdlib function abs for?

A

For producing the absolute value of a number type int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What do you use the cstdlib function labs for?

A

For producing the absolute value of a number type long.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What do you use the cmath function fabs for?

A

For producing the absolute value of a number type double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What must you include and use in order to write 2^3 = 8 in C++?

A

You need to use the cmath library, and use the function pow(x,y).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you round up numbers?

A

With the cmath function ceil(); Only works on doubles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you round down numbers?

A

With the cmath function floor(); Only works on doubles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you end the program from a place in your code?

A

With the cstdlib function exit(1); Only works on integers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you produce a “semi-random” number?

A

With the cstdlib function rand();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you set the seed (starting value) for rand?

A

With the cstdlib function srand();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Are there any restrictions on the use of the pow( ) function?

A

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.

17
Q

What integer should you use as an argument to the exit ( ) function?

A

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.

18
Q

All standard functions have argument types they’re meant for. What happens if you use the wrong type?

A

In many cases, some automatic type conversion will be done for you by C++. However, the results may not be what you intended.

19
Q

What numbers will you have a chance of getting if you use the function rand ( ) ?

A

Any number in the range 0 to at least 32767. (32767 is the maximum two-byte positive integer).

20
Q

How do you scale the rand ( ) function, i.e. to give you random numbers between 0 and 10?

A

You have to use:

rand ( ) % 11.

21
Q

Why is the rand ( ) function not truly random?

A

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”.

22
Q

Define: local variable.

A

Variables that are declared within the body of a function definition are called local variables.

23
Q

What is a function declaration? Give an example.

A
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);
24
Q

What is a good way to make a function declaration comment?

A

One good way to write a function declaration comment is to break it down into two kinds of information called the precondition and postcondition.

25
Q

Is it necessary to return 0 from the main function?

A

The C++ standard says that you can omit the return 0 statement in the main part of the program, but many compilers still require it and almost all compilers allow you to include it. For the sake of portability, you should include a return + statement.

26
Q

What is meant by treating the function as a black box?

A

It’s a rule of programming. A programmer who uses a function is a program needs to know what the function does, but should not need to know how the function accomplishes the task.

27
Q

What are the two rules for writing a black-box function definition?

A
  1. The function declaration comment should tell the programmer any and all conditions that are required of the arguments to the function and should describe the result of a function invocation.
  2. All variables used in the function body should be declared in the function body.
28
Q

What is a global named constant, and should you use them?

A

A constant that is declared at the beginning of the program, outside the body of all the functions. Can be useful.

29
Q

What is a global variable?

A

A variable that is declared at the beginning of the program, outside the body of all the functions. They are seldom useful, and can make a program harder to understand and maintain.

30
Q

What is a block?

A

A block is some C++ code enclosed in braces { }. The variables in a block are local to the block, and so the variable names can be used outside the block for something else.