Download presentation
Presentation is loading. Please wait.
Published byKristina Townsend Modified over 9 years ago
1
Beginning C++ Through Game Programming, Second Edition by Michael Dawson
2
Chapter 5 Functions: Mad Lib
3
Objectives Write new functions Accept values into your new functions through parameters Return information from your new functions through return values Work with global variables and constants Overload functions Inline functions
4
Creating Functions Can write multiple functions Like functions that are part of the standard language, your new functions perform a task and then return control One advantage is you can break up your code into manageable pieces
5
Declaring Functions Declare it before you can call it One way to declare is to write a function prototype A prototype lists return value (or void ), name of the function, list of parameters between a set of parentheses Parameters receive values sent as arguments
6
Function Prototypes void instructions(); Declares a function named instructions that doesn’t return a value and takes no values Can also let function definition act as declaration Good practice to use prototypes
7
Defining Functions Writing the code that makes the function tick Function header is return value (or void ), name, a list of parameters between a set of parentheses Function body is a block with curly braces that contains function instructions void instructions() { cout << "Welcome!\n"; cout << "Here's how to play...\n"; }
8
Calling Functions Call your own functions like any other instructions(); Control jumps to that function; when finished, control returns to the calling code.
9
Abstraction Lets you think about the big picture without worrying about the details Calling functions is practicing abstraction One fast-food employee tells the another that he just filled a #3, and “sized it”—both understand what that means
10
Parameters and Return Values Can provide a function value and get a value back Allows your functions to communicate with the rest of your program
11
Returning a Value Return a value from a function to send information back to the calling code Specify a return type when you declare the function Return a value of that type from the function with the return statement A function ends whenever it hits a return statement
12
Example: Returning a Value char askYesNo1() { char response1; do { cout << "Please enter 'y' or 'n': "; cin >> response1; } while (response1 != 'y' && response1 != 'n'); return response1; }
13
Accepting Values into Parameters Parameters act like local variables Accepts a string object as a parameter and names that parameter question char askYesNo2(string question) { char response2; do { cout << question << " (y/n): "; cin >> response2; } while (response2 != 'y' && response2 != 'n'); return response2 ; }
14
Encapsulation Helps keep independent code truly separate by encapsulating the details Variables you create in a function, including function parameters, can’t be directly accessed outside the function Use parameters and return values to communicate information between functions A TV with a remote is an example of encapsulation
15
Software Reuse Functions from one program can be used in another –Increased company productivity –Improved software quality –Improved software performance
16
Scopes A variable’s scope determines where the variable can be seen in your program Limits the accessibility of variables Key to encapsulation
17
Separate Scopes Every block is scope (e.g., functions) Variables declared in a scope aren’t visible outside of that scope Variables declared in a function aren’t visible outside of that function Variables declared inside a function are considered local variables Parameters act just like local variables in functions
18
Nested Scopes A scope inside another scope If a variable hasn’t been declared in a scope, the computer looks up the levels of nested scopes to find the variable Although you can declare variables with the same name in a series of nested scopes, it’s not a good idea When you define variables inside for loops, while loops, if statements, and switch statements, these variables don’t exist outside their structures
19
Global Variables Declare global variables outside of any function in your program file Accessible in any part of program Defy encapsulation Hide a global variable if declared a new variable with the same name in some scope Minimize your use of global variables
20
Example: Global Variable #include using namespace std; int glob = 10; // global variable int main() { cout << glob; return 0; }
21
Global Constants Constants that can be accessed from anywhere in your program Can help make programs clearer Declare outside of any function
22
Example: Global Constant #include using namespace std; // global constant const int MAX_ENEMIES = 10; int main() { cout << MAX_ENEMIES; return 0; }
23
Default Arguments A default value for an argument if none passed Can specify a default argument in a function prototype Use an equal sign (=) after a parameter name followed by default value
24
Example: Default Arguments int askNumber(int high, int low = 1); If a value isn’t passed to low, it's assigned 1 Once you specify a default argument in a list of parameters, you must specify default arguments for remaining parameters Don’t repeat the default argument in the function definition
25
Assigning Default Arguments to Parameters int number = askNumber(5); In the function, high is assigned 5 and low gets the default value of 1 In a function call, once you omit an argument, you must omit arguments for remaining parameters
26
Overriding Default Arguments int number = askNumber(10, 5); Can pass arguments to parameters with default argument values Values you pass will override default values In this case, low is assigned 5
27
Overloading Functions Use function overloading so that a single function can handle arguments of different types
28
Creating Overloaded Functions Write multiple function definitions with the same name and different parameter lists Function prototypes that overload triple() int triple(int number); string triple(string text);
29
Calling Overloaded Functions The definition that corresponds to type of arguments passed will be called cout << triple(5); cout << triple("gamer");
30
Inlining Functions A small performance cost associated with calling a function Inlining is asking compiler to make a copy of the function wherever it's called Possible to speed up performance with inlining But can lead to worse performance Compiler might not even inline
31
Specifying Functions for Inlining To mark a function for inlining, put inline before the function definition inline int radiation(int health) Note that you don’t use inline in the function declaration int radiation(int health);
32
Summary Functions allow you to break up your programs into manageable chunks One way to declare a function is to write a function prototype Defining a function means writing all the code that makes the function tick You can use the return statement to return a value from a function You can also use return to end a function that has void as its return type
33
Summary (cont.) A variable’s scope determines where the variable can be seen in your program Global variables are accessible from any part of your program Global constants are accessible from any part of your program Default arguments are assigned to a parameter if no value for the parameter is specified in the function call
34
Summary (cont.) Function overloading is the process of creating multiple definitions for the same function, each of which has a different set of parameters Function inlining is the process of asking the compiler to inline a function—meaning the compiler should make a copy of the function everywhere in the code where the function is called Inlining very small functions can sometimes yield a performance boost
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.