by Pearson Education, Inc. All Rights Reserved. 2

Similar documents
C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 7 Array. Array. C++, How to Program

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

Cpt S 122 Data Structures. Introduction to C++ Part II

Functions and Recursion

Fundamentals of Programming Session 25

Object Oriented Design

C: How to Program. Week /Apr/23

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Introduction to C++ Introduction to C++ 1

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Multiple-Subscripted Arrays

Fundamentals of Programming Session 23

Functions. Introduction :

Programming for Engineers Arrays

Arrays. Systems Programming Concepts

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

Review of the C Programming Language for Principles of Operating Systems

Arrays. Week 4. Assylbek Jumagaliyev

Introduction to C++ Systems Programming

C Programming for Engineers Functions

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

LECTURE 06 FUNCTIONS

IT 374 C# and Applications/ IT695 C# Data Structures

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

CS1150 Principles of Computer Science Methods

Exam 3 Chapters 7 & 9

IS 0020 Program Design and Software Tools

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Short Notes of CS201

Functions. Arizona State University 1

BITG 1113: Function (Part 2) LECTURE 5

Generics Pearson Education, Inc. All rights reserved.

CS201 - Introduction to Programming Glossary By

Chapter 7 Functions. Now consider a more advanced example:

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

by Pearson Education, Inc. All Rights Reserved. 2

6.5 Function Prototypes and Argument Coercion

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Review of the C Programming Language

Functions, Arrays & Structs

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program)

C Functions. Object created and destroyed within its block auto: default for local variables

Chapter 4 - Arrays. 4.1 Introduction. Arrays Structures of related data items Static entity (same size throughout program) A few types

Chapter 2, Part I Introduction to C Programming

Lecture 5: Methods CS2301

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

C++ PROGRAMMING SKILLS Part 4: Arrays

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

Functions, Arrays & Structs

Introduction to Programming Using Java (98-388)

KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

CS1150 Principles of Computer Science Methods

Lecture 04 FUNCTIONS AND ARRAYS

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 4

1 Lexical Considerations

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Chapter 15 - C++ As A "Better C"

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming Session 19

Chapter 6: Functions

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Object-Oriented Design (OOD) and C++

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

Chapter 3: Modules. Starting Out with Programming Logic & Design. Second Edition. by Tony Gaddis

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

EECS168 Exam 3 Review

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Objectivities. Experiment 1. Lab6 Array I. Description of the Problem. Problem-Solving Tips

LECTURE 02 INTRODUCTION TO C++

0. Overview of this standard Design entities and configurations... 5

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object Oriented Methods : Deeper Look Lecture Three

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CSI33 Data Structures

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Lecture 18 Tao Wang 1

Ch. 12: Operator Overloading

Transcription:

Two ways to pass arguments to functions in many programming languages are pass-by-value and pass-by-reference. When an argument is passed by value, a copy of the argument s value is made and passed (on the function call stack) to the called function. Changes to the copy do not affect the original variable s value in the caller. To specify a reference to a constant, place the const qualifier before the type specifier in the parameter declaration. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 2

1992-2010 by Pearson Education, Inc. All Rights Reserved. 3

With pass-by-reference, the caller gives the called function the ability to access the caller s data directly, and to modify that data. A reference parameter is an alias for its corresponding argument in a function call. To indicate that a function parameter is passed by reference, simply follow the pa-rameter s type in the function prototype by an ampersand (&); use the same convention when listing the pa-rameter s 1992-2010 type by in Pearson the Education, function Inc. All Rights Reserved. header. 4

1992-2010 by Pearson Education, Inc. All Rights Reserved. 5

1992-2010 by Pearson Education, Inc. All Rights Reserved. 6

1992-2010 by Pearson Education, Inc. All Rights Reserved. 7

1992-2010 by Pearson Education, Inc. All Rights Reserved. 8

1992-2010 by Pearson Education, Inc. All Rights Reserved. 9

1992-2010 by Pearson Education, Inc. All Rights Reserved. 10

References can also be used as aliases for other variables within a function. Reference variables must be initialized in their declarations and cannot be reassigned as aliases to other variables. Once a reference is declared as an alias for another variable, all opera-tions supposedly performed on the alias are actually performed on the original variable. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 11

1992-2010 by Pearson Education, Inc. All Rights Reserved. 12

1992-2010 by Pearson Education, Inc. All Rights Reserved. 13

Functions can return references, but this can be dangerous. When return-ing a reference to a variable declared in the called function, the variable should be declared static in that function. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 14

1992-2010 by Pearson Education, Inc. All Rights Reserved. 15

It s possible to declare local and global variables of the same name. C++ provides the unary scope resolution operator (::) to access a global variable when a local variable of the same name is in scope. Using the unary scope resolution operator (::) with a given variable name is optional when the only variable with that name is a global variable. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 16

1992-2010 by Pearson Education, Inc. All Rights Reserved. 17

1992-2010 by Pearson Education, Inc. All Rights Reserved. 18

C++ enables several functions of the same name to be defined, as long as they have different signatures. This is called function overloading. The C++ compiler selects the proper function to call by examining the number, types and order of the arguments in the call. Function overloading is used to create sev-eral functions of the same name that perform similar tasks, but on different data types. 1992-2010 by Pearson Education, Inc. All Rights Reserved. 19

1992-2010 by Pearson Education, Inc. All Rights Reserved. 20

1992-2010 by Pearson Education, Inc. All Rights Reserved. 21

Overloaded functions are distinguished by their signatures. A signature is a combination of a function s name and its parameter types (in order). 1992-2010 by Pearson Education, Inc. All Rights Reserved. 22

This chapter introduces the important topic of data structures collections of related data items. Arrays are data structures consisting of related data items of the same type. After discussing how arrays are declared, created and initialized, we present a series of practical examples that demonstrate several common array manipulations. 1992-2010 by Pearson Education, Inc. All Rights Reserved.

An array is a consecutive group of memory locations that all have the same type. To refer to a particular location or element in the array, spec-ify the name of the array and the position number of the particular element. Figure 7.1 shows an integer array called c. 12 elements. The position number is more formally called a subscript or index (this number specifies the number of elements from the beginning of the array). The first element in every array has subscript 0 (zero) and is sometimes called the zeroth element. The highest subscript in array c is 11, which is 1 less than the number of elements in the array (12). A subscript must be an integer or integer expression (using any integral type). 1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

Arrays occupy space in memory. To specify the type of the elements and the number of elements required by an array use a declaration of the form: type arrayname[ arraysize ]; The compiler reserves the appropriate amount of memory. Arrays can be declared to contain values of any nonreference data type. 1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

The elements of an array also can be initialized in the array declaration by following the array name with an equals sign and a brace-delimited comma-separated list of initializers. The program in Fig. 7.4 uses an initializer list to initialize an integer array with 10 values (line 10) and prints the array in tabular format (lines 12 16). If there are fewer initializers than elements in the array, the remaining array elements are initialized to zero. If the array size is omitted from a declaration with an initializer list, the compiler determines the number of elements in the array by counting the number of elements in the initializer list. 1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

Figure 7.5 sets the elements of a 10-element array s to the even inte-gers 2, 4, 6,, 20 (lines 14 15) and prints the array in tabular format (lines 17 21). Line 10 uses the const qualifier to declare a so-called constant variable arraysize with the value 10. Constant vari-ables must be initialized with a constant expression when they re declared and cannot be modified thereafter. Constant variables are also called named constants or read-only variables. 1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

Often, the elements of an array represent a series of values to be used in a calculation. The program in Fig. 7.8 sums the values contained in the 10-element integer ar-ray a. 1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved.

1992-2010 by Pearson Education, Inc. All Rights Reserved. 43