C++ Book


Questions and Answers

Q: Why should I write functions?

A: Functions allow you to break up your programs into logical pieces. These pieces result in smaller, more manageable chunks of code, which are easier to work with than a single monolithic program.

Q: What’s encapsulation?

A: At its core, encapsulation is about keeping things separate. Function encapsulation provides that variables declared in a function are not accessible outside the function, for example.

Q: What’s the difference between an argument and a parameter?

A: An argument is what you use in a function call to pass a value to a function. A parameter is what you use in a function definition to accept values passed to a function.

Q: Can I have more than one ‘return’ statement in a function?

A: Sure. In fact, you might want multiple ‘return’ statements to specify different end points of a function.

Q: What’s a local variable?

A: A variable that’s defined in a scope. All variables defined in a function are local variables; they’re local to that function.

Q: What does it mean to hide a variable?

A: A variable is hidden when you declare it inside a new scope with the same name as a variable in an outer scope. As a result, you can’t get to the variable in the outer scope by using its variable name in the inner scope.

Q: When does a variable go out of scope?

A: A variable goes out of scope when the scope in which it was created ends.

Q: What does it mean when a variable goes out of scope?

A: It means the variable ceases to exist.

Q: What’s a nested scope?

A: A scope created within an existing scope.

Q: Must an argument have the same name as the parameter to which it’s passed?

A: No. You’re free to use different names. It’s only the value that’s passed from a function call to a function.

Q: Can I write one function that calls another?

A: Of course. In fact, whenever you write a function that you call from ‘main()’, you’re doing just that. In addition, you can write a function (other than ‘main()’) that calls another function.

Q: What is code profiling?

A: It’s the process of recording how much CPU time various parts of a program use.

Q: Why profile code?

A: To determine any bottlenecks in a program. Sometimes it makes sense to revisit these sections of code in an attempt to optimize them.

Q: When do programmers profile code?

A: Usually toward the end of the programming of a game project.

Q: What’s premature optimization?

A: An attempt to optimize code too early in the development process. Code optimization usually makes sense near the end of programming a game project.


Discussion Questions

  1. How does function encapsulation help you write better programs?
  2. How can global variables make code confusing?
  3. How can global constants make code clearer?
  4. What are the pros and cons of optimizing code?
  5. How can software reuse benefit the game industry?