Chapter 4 void Functions

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Introduction to Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
C++ for Engineers and Scientists Third Edition
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 © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
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.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
User-Written Functions
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 3: Using Methods, Classes, and Objects
by Tony Gaddis and Godfrey Muganda
Deitel- C:How to Program (5ed)
CSCI 161: Introduction to Programming Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
Functions Inputs Output
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Starting Out with Java: From Control Structures through Objects
Functions.
6 Chapter Functions.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Topics Introduction to Functions Defining and Calling a Function
Starting Out with Programming Logic & Design
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 2042)
CPS125.
Presentation transcript:

Chapter 4 void Functions Starting Out with Games & Graphics in C++ Tony Gaddis

4.1 Modularizing a Program with Functions Concept: A large program can be broken up into manageable functions that each performs a major task.

4.1 Modularizing a Program with Functions A function is a group of programming statements that has a name There are two general types of functions: Those that return a value Known as value-returning functions For example dbRND Those that do not Known as void functions dbDot dbLine dbBox

4.1 Modularizing a Program with Functions Functions can be used to break a program down into smaller, manageable pieces This approach is sometimes called divide and conquer Figure 4-1 Using functions to divide and conquer a large task

4.1 Modularizing a Program with Functions Benefits of Using Functions Simpler Code A program’s code tends to be simpler and easier to understand when it is broken down into functions Code Reuse Functions reduce the duplication of code within a program Better Testing When each task within a program is contained in its own function, testing and debugging becomes simpler Faster Development Functions can be written for commonly needed tasks, and those functions can be incorporated into each program that needs them Easier Facilitation of Teamwork Different programmers can be assigned the task of writing different functions

4.2 Defining and Calling a void Function Concept: The code for a function is known as a function definition. To execute a function you write a statement that calls it.

4.2 Defining and Calling a void Function Function Names When naming a function, C++ requires you follow the same rules for naming variables, which we recap here: Function names must be One word No spaces First character must be Letter a – z, A – Z, or underscore (_) After first character Letters a – z, A – Z, numbers 0 – 9, or underscore(_) Uppercase and lowercase characters are distinct LineLength is not the same as linelength

4.2 Defining and Calling a void Function Function Names Because functions perform actions, programmers commonly use verbs in function names For example drawSmileyFace setDrawingColor displayCaption clearScreen Function names should describe what the function does

4.2 Defining and Calling a void Function Defining a void Function To create a function you write its definition Here is the general format of a void function definition:

4.2 Defining and Calling a void Function Defining a void Function The first line is known as the function header It marks the beginning of the function The header for a void function begins with the keyword void Followed by the function name Followed by a set of parentheses Notice that the function header does not end with a semicolon

4.2 Defining and Calling a void Function Defining a void Function After the function header, a set of statements enclosed in curly braces appears This set of statements is known as the body of the function These are the statements that execute when the function is called

4.2 Defining and Calling a void Function Defining a void Function Let’s look at an example of a void function definition: This code defines a function named displayMessage The body of the function contains two statements Notice the statements are indented, this is not required, but makes the code easier for people to read

4.2 Defining and Calling a void Function Calling a Function A function definition specifies what a function does, but it does not cause the function to execute To execute a function, you must call it This is how we would call the displayMessage function:

4.2 Defining and Calling a void Function Calling a Function To call a function, you Write its name Followed by a set of parentheses Followed by a semicolon Notice that you do not write the word void in the function call statement

4.2 Defining and Calling a void Function Calling a Function When a function is called the program jumps to that function executes the statements inside it Programmers commonly say that control of the program transfers to the function This simply means that the function takes control of the programs execution. Figure 4-3 Calling the displayMessage function

4.2 Defining and Calling a void Function Calling a Function When the end of a function is reached the program jumps back to the statement that called the function execution resumes with the very next statement When this happens, we say that the function returns Figure 4-4 The displayMessage function returns

4.2 Defining and Calling a void Function Function Prototypes In C++, a function’s definition must appear in the file before any statements that call the function, or a compiler error will occur The only exception is when you use function prototypes A function prototype is a statement that declares the existence of a function, but does not define the function

4.2 Defining and Calling a void Function Function Prototypes A function prototype is Merely a way of telling the compiler that a particular function exists in the program, and its definition appears at a later point This is how function prototypes for the displayMessage and pauseProgram functions would look:

4.2 Defining and Calling a void Function Function Prototypes Look like function headers, except a semicolon appears at the end Are usually placed near the top of a program so the compiler will encounter them before any function calls Are used by most C++ programmers Allow you to write the DarkGDK function at the top of your source code file

4.2 Defining and Calling a void Function Function Prototypes Function Prototypes Function Calls Function Definitions

4.3 Designing a Program to Use Functions Concept: Programmers commonly use a technique known as top-down design to break down a program into functions that each perform a single task.

4.3 Designing a Program to Use Functions Programmers commonly use a technique known as top-down design to break a program down into functions The process of top-down design is performed in the following manner: Overall task is broken down into a series of subtasks Subtasks are examined and are broken down into more subtasks This step is repeated until no more subtasks can be identified Subtasks are written into code This process is called top-down design because the programmer begins by looking at the topmost level of tasks that must be performed, and then breaks down those tasks into lower levels of subtasks

4.3 Designing a Program to Use Functions Hierarchy Charts A hierarchy chart, which is also known as a structure chart, shows boxes that visually represent each function in a program Figure 4-8 Hierarchy chart

4.4 Local Variables Concept: A local variable is declared inside a function and cannot be accessed by statements that are outside the function. Different functions can have local variables with the same names because the functions cannot see each other’s variables.

4.4 Local Variables A variable that is declared inside a function is called a local variable A local variable Belongs to the function in which it is declared Can only be accessed by statements inside the function in which it is declared The term local is meant to indicate that the variable can be used only locally, within the function in which it is declared

4.4 Local Variables DarkGDK function local variables An error will occur if a statement in one function tries to access a local variable that belongs to another function DarkGDK function local variables not visible to drawBlueBox function

4.4 Local Variables Scope and Local Variables Programmers commonly use the term scope to describe the part of a program in which a variable may be accessed A variable is visible only to statements inside the variable’s scope A local variable’s scope Begins at the variable’s declaration Ends at the end of the function in which the variable is declared A local variable cannot be accessed by statements that are Outside the function Inside the function, but before the variable’s declaration

4.4 Local Variables Duplicate Variable Names You cannot have two variables with the same name in the same scope A function cannot have two local variables with the same name You can have local variables with the same name declared in separate functions

4.5 Passing Arguments to Functions Concept: An argument is a piece of data that is passed into a function when the function is called. When an argument is passed into a function, a special variable in the function known as a parameter receives the argument.

4.5 Passing Arguments to Functions Pieces of data that are sent into a function are known as arguments The function can use its arguments in calculations or other operations If you want a function to receive arguments when it is called you must equip the function with one or more parameter variables A parameter variable is a special variable that is assigned the value of an argument when a function is called A parameter variable is often simply called a parameter

4.5 Passing Arguments to Functions Let’s look at an example of a function that uses a parameter variable to accept an argument: When you call the function, you pass an argument indicating the circle’s radius Parameter variable declaration The parameter variable’s Declaration is inside the parentheses of the function header Purpose is to receive an integer argument when the function is called

4.5 Passing Arguments to Functions Function prototype Function call Function definition

4.5 Passing Arguments to Functions Notice the function prototype for the drawCircle function does not list the name of the parameter variable It is not necessary to list the name of the parameter variable inside the parentheses Only the data type is required Listing a parameter name will not cause an error The compiler merely ignores parameter name

4.5 Passing Arguments to Functions When the drawCircle function is called The number 100 appears inside the parentheses 100 is the argument that is being passed to the function When the function executes, 100 will be assigned to radius Figure 4-10 the argument 100 is assigned to the radius function

4.5 Passing Arguments to Functions Parameter Variable Scope Earlier in this section, you learned A variable’s scope is the part of the program in which the variable may be accessed A variable is visible only to statements inside the variable’s scope A parameter variable’s scope is the entire function in which the parameter is declared It is visible only to statements inside the function

4.5 Passing Arguments to Functions Passing Multiple Arguments If a function needs multiple arguments You can equip it with multiple parameter variables Notice, two parameter variables, num1 and num2 are declared inside the parentheses This is often referred to as a parameter list Also notice a comma separates the declarations Figure 4-13 Two arguments passed into two parameters

4.5 Passing Arguments to Functions Passing Arguments by Value All of the programs we have looked at so far pass arguments by value Arguments and parameter variables are separate items in memory Passing an argument by value means that only a copy of the argument’s value is passed into the parameter variable If the parameter variable is changed inside the function It has no effect on the argument in the calling part of the program

4.5 Passing Arguments to Functions Passing Arguments by Value Program 4-10 (PassByValue.cpp) partial listing The changeMe function can only modify a copy of the number variable, not the number variable itself

4.5 Passing Arguments to Functions Passing Arguments by Value Passing an argument is a way that one function can communicate with another function When the argument is passed by value: The communication works in one direction The calling function can use the argument to communicate with the called function The called function cannot use the argument to communicate with the calling function Figure 4-15 Example output of Program 4-10

4.5 Passing Arguments to Functions Passing Arguments by Reference Passing an argument by reference means that the argument is passed into a special type of parameter known as a reference variable A reference variable allows the argument to be modified in the calling function A reference variable acts as an alias for the variable that was passed as an argument A reference variable references the other variable Anything you do to a reference variable is actually done to the variable it references

4.5 Passing Arguments to Functions Passing Arguments by Reference Reference variables are useful for establishing two-way communication between functions Communication can take place in the following ways: The calling function can communicate with the called function by passing an argument The called function can communicate with the calling function by modifying the value of the argument via the reference variable

4.5 Passing Arguments to Functions Passing Arguments by Reference In C++, you declare a reference variable by placing an ampersand (&) before the variable’s name For example, look at the following function: Inside the parentheses, the & indicates that value is a reference variable The function assigns 0 to the value parameter Because value is a reference variable, this action is actually performed on the variable that was passed to the function as an argument

4.5 Passing Arguments to Functions Passing Arguments by Reference When using a reference parameter variable Include the ampersand (&) after the data type in the function prototype Here is the prototype for the setToZero function:

4.5 Passing Arguments to Functions Passing Arguments by Reference Function prototype Variable declarations Function Calls Function Definition

4.5 Passing Arguments to Functions Passing Arguments by Reference Variables a, b, and c are initialized with the value 99 Those variables are passed as arguments to the setToZero function Each time the function is called, the variable that is passed as an argument is set to 0 Figure 4-16 Output of Program 4-11

4.5 Passing Arguments to Functions Passing Arguments by Reference

4.6 Global Variables and Constants Concept: A global variable is accessible to all the functions in a program.

4.6 Global Variables and Constants A global variable is declared outside of all the functions in a program A global variable’s scope Begins at the variable’s declaration Ends at the end of the program A global variable can be accessed by all functions that are defined after the variable is declared

4.6 Global Variables and Constants Global variable declaration Accessed by all functions

4.6 Global Variables and Constants You should be careful when using global variables Global variables can: Make debugging difficult Any statement can change the value Programs can contain thousands of statements Limit code reusability Functions that use global variables are dependent on those values and must be redesigned before they can be used in another program Make a program hard to understand You must be aware of all the parts that access the variable With global variables, this can be the entire program

4.6 Global Variables and Constants Global Constants A global constant is a named constant that is available to every function in the program A global constant: Contains a value that cannot be changed during the program’s execution Reduces many of the potential hazards that are associated with global variables Can be used to represent unchanging values that are needed throughout the program

4.6 Global Variables and Constants Global Constants Global constant declarations

4.6 Global Variables and Constants Global Constants Used in a function And in another function

Chapter 4 void Functions QUESTIONS ?