< Previous | Contents | Next >

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: Whats 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: Whats 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.

Questions and Answers 185


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: Whats a local variable?

A: A variable thats defined in a scope. All variables defined in a function are local variables; theyre 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 cant 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: Whats a nested scope?

A: A scope created within an existing scope.

Q: Must an argument have the same name as the parameter to which its passed?

A: No. Youre free to use different names. Its only the value thats 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(), youre doing just that. In addition, you can write a function (other than main()) that calls another function.

Q: What is code profiling?

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

186 Chapter 5 n Functions: Mad Lib


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: Whats 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.