Beginning C++ Through Game Programming, Second Edition by Michael Dawson.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Guide to Programming with Python
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Using C++ Functions Object-Oriented Programming Using C++ Second Edition 4.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
C++ for Engineers and Scientists Third Edition
Lecture 3 Using C++ Functions COSC1567 C++ Programming.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Beginning C++ Through Game Programming, Second Edition
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Fundamental Programming Fundamental Programming Introduction to Functions.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 Modularity Using Functions
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Chapter 7: User-Defined Functions II
Chapter 5 - Functions Outline 5.1 Introduction
Introduction to Functions
User Defined Functions
6 Chapter Functions.
Object-Oriented Programming Using C++ Second Edition
Namespaces How Shall I Name Thee?.
Chapter 9: Value-Returning Functions
Fundamental Programming
Presentation transcript:

Beginning C++ Through Game Programming, Second Edition by Michael Dawson

Chapter 5 Functions: Mad Lib

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

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

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

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

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"; }

Calling Functions Call your own functions like any other instructions(); Control jumps to that function; when finished, control returns to the calling code.

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

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

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

Example: Returning a Value char askYesNo1() { char response1; do { cout << "Please enter 'y' or 'n': "; cin >> response1; } while (response1 != 'y' && response1 != 'n'); return response1; }

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 ; }

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

Software Reuse Functions from one program can be used in another –Increased company productivity –Improved software quality –Improved software performance

Scopes A variable’s scope determines where the variable can be seen in your program Limits the accessibility of variables Key to encapsulation

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

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

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

Example: Global Variable #include using namespace std; int glob = 10; // global variable int main() { cout << glob; return 0; }

Global Constants Constants that can be accessed from anywhere in your program Can help make programs clearer Declare outside of any function

Example: Global Constant #include using namespace std; // global constant const int MAX_ENEMIES = 10; int main() { cout << MAX_ENEMIES; return 0; }

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

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

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

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

Overloading Functions Use function overloading so that a single function can handle arguments of different types

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

Calling Overloaded Functions The definition that corresponds to type of arguments passed will be called cout << triple(5); cout << triple("gamer");

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

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

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

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

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