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

Similar documents
C++ Programming Chapter 7 Pointers

Introduction to Computer Science Midterm 3 Fall, Points

What is an algorithm?

Homework #3 CS2255 Fall 2012

Pointers! Arizona State University 1

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Lecture 05 POINTERS 1

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

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CSC 211 Intermediate Programming. Arrays & Pointers

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

by Pearson Education, Inc. All Rights Reserved.

C Pointers. 7.2 Pointer Variable Definitions and Initialization

CS2255 HOMEWORK #1 Fall 2012

Review of the C Programming Language for Principles of Operating Systems

Chapter 9: Pointers. Copyright 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved.

Chapter 9: Getting the Address of a Variable. Something Like Pointers: Arrays. Pointer Variables 8/23/2014. Getting the Address of a Variable

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Pointers and Strings Prentice Hall, Inc. All rights reserved.

Arrays. Week 4. Assylbek Jumagaliyev

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CS201 Some Important Definitions

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Review of the C Programming Language

Functions and Recursion

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Exam 3 Chapters 7 & 9

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

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

IS 0020 Program Design and Software Tools

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

Pointers. Variable Declaration. Chapter 10

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

CSI33 Data Structures

Lecture 23: Pointer Arithmetic

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

Objectives. In this chapter, you will:

C++ PROGRAMMING SKILLS Part 4: Arrays

C++ Programming: From Problem Analysis to Program Design, Third Edition

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

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

Chapter 6 Pointers and Arrays

UNIT- 3 Introduction to C++

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

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

The C++ Language. Arizona State University 1

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Absolute C++ Walter Savitch

7.1 Optional Parameters

CS201- Introduction to Programming Current Quizzes

Functions, Arrays & Structs

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

Pointers, Dynamic Data, and Reference Types

1 Lexical Considerations

Functions, Arrays & Structs

Abstract Data Type (ADT) & ARRAYS ALGORITHMS & DATA STRUCTURES I COMP 221

Week 3: Pointers (Part 2)

Functions. Introduction :

Introduction to C++ Systems Programming

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

BITG 1233: Introduction to C++

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Tokens, Expressions and Control Structures

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

A Short Summary of Javali

Review of Important Topics in CS1600. Functions Arrays C-strings

[0569] p 0318 garbage

Pointers and Strings. Adhi Harmoko S, M.Komp

Lexical Considerations

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

Pointers and Dynamic Memory Allocation

Chapter 6 - Pointers

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

Pointers. Reference operator (&) ted = &andy;

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

CS201 Latest Solved MCQs

FORM 2 (Please put your name and form # on the scantron!!!!)

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

CSC 211 Intermediate Programming. Pointers

Chapter 1: Object-Oriented Programming Using C++

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology 8/23/2014. Arrays Hold Multiple Values

Arrays. Arizona State University 1

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

12. Pointers Address-of operator (&)

Short Notes of CS201

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

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

Transcription:

Chapter 6 - Functions return type void or a valid data type ( int, double, char, etc) name parameter list void or a list of parameters separated by commas body return keyword required if function returns a value; optional if it does not int square ( int x ) return x * x; void ErrorMsg ( ) cout << \n\nan error occurred. The program is exiting \n\n ; exit( EXIT_FAILURE ); parameters are the names of the local variables defined in the function header. arguments are the names of the variables/expressions used in the function call ( invocation ). calling a function - function-name(argument list); y = square ( z ); ErrorMsg( ); include parentheses even if parameter list is empty! function prototypes int square ( int ); void main ( ) y = square ( x ); you may optionally include parameter variable names in the prototype void ShowSum ( int x, int y, int z); or void ShowSum ( int, int, int); you must place either the function definition or prototype ahead of all calls to the function call by value - a copy of an argument is passed to the calling function z = 10; y = square ( z ); 10 ( copy of the value of the variable z ) is sent to function square return statement return; function does not return a value return <expression>; function returns a value returns control to calling function or OS if called in function main Returning a value from a function A function may have any number of inputs but may return only a single value ( or nothing at all, signified by the keyword void for the return type ). return <espression>; int Square ( int x ) return x * x; // return the square of x.

local and global variables a local variable is declared inside a function and is local to that function a global variable is declared outside of all functions. The scope of a global variable is the point of declaration to the end of the program local variables have the automatic storage class automatically created and destroyed not automatically initialized global variables have the static storage class they exist for the life of the program if not explicitly initialized, they are implicitly initialized to 0 local and global variables with the same name - unary scope resolution operator ( :: ) In the absence of the unary scope resolution operator, a local variable with the same name as a global variable hides the global variable. int Number = 100; int main ( ) int Number = 10; cout << Number in main: << Number <<, global Number: << ::Number; Displays: Number in main: 10, global Number: 100 static local variables [ variables declared static within a function ] use the storage class qualifier static static int x; static storage class if not explicitly initialized, implicitly initialized to 0 initialized only once persistent between function calls ( retain their values ) default arguments passed to parameters automatically if no argument is provided in the function call constant values with an = operator in front of them specified in the function prototype if present, otherwise in the function header when an argument is left out of a function call, all arguments that come after it must be left out as well double area ( double=0, double=0 ); int main ( ) double length = 5.5, width = 7.5; cout << the area of the rectangle is: << area( ); cout << the area of the rectangle is: << area(length); cout << the area of the rectangle is: << area(length, width); in the function definition, default arguments must be specified after all required parameters int Strange ( int, int, int=0, int=0, int=0); function has 2 required parameters and 3 optional parameters

reference parameters reference parameters allow a function to access the parameter s original argument. changes made to the parameter are also made to the original argument the & operator is used to declare a reference parameter void square ( int &x ); // call by reference using a reference int main ( ) int x = 5; cout << Print the value of x is: << x << endl; square ( x ); cout << Print the square of x is: << x << endl; void square ( int &y ) y *= y; overloaded functions functions with the same name that have different parameter lists int square ( int ); double square ( double ); function with the same name may have the same return type but MUST have different parameter lists exit ( ) function causes a program to terminate regardless of which function is currently executing to use, you must include the header file cstdlib requires an integer value as an argument can be used with the symbolic constants EXIT_SUCCESS and EXIT_FAILURE exit ( EXIT_SUCCESS ); exit ( EXIT_FAILURE ); Chapter 7 - Arrays An array is an aggregate data type. It can store a group of values, all of the same type. Arrays are declared similar to other variables, except that they must have a size declarator or an initializer list that is used to determine how many elements to allocate space for. The size declarator must be a constant integer expression ( integer literal or integer named constant ) with a value greater than 0. Example declarations: int Scores[50]; const int size = 50; int Scores[size]; // define an array of 50 integers using a constant literal // define a named integer constant // use named integer constant as a size declarator Arrays are like any automatic variables. If they are not initialized, the elements contain garbage. When referencing array elements, you use a subscript enclosed in square brackets. Subscript numbers are in the range of 0 to array size 1 because in C++ you start counting at 0! ( zero-based addressing ). int X[10]; // 10 element array of integers; subscripts are 0.. 9 X [0] X [1] X [2] X [3] X [4] X [5] X [6] X [7] X [8] X [9]

To work with a single array element, use the array name and a subscript value. The value can be an integer variable or expression. int temps[5]; int k=3; temps[3] = 81; or temps[k] = 81; C++ does not do any bounds checking. If array Count is declared to have 10 elements, you can refer to Count [10] or Count[-1] - both of which are outside of the bounds of the array. Arrays can be initialized when they are declared using an initializer list. int Odds[5] = 1, 3, 5, 7, 9 ; The initializer list may specify fewer than the number of elements in the array. You may not skip elements in the initializer list. Uninitialized elements are initialized to 0. int Odds[5] = 1, 3 ; // elements 2, 3, and 4 are initialized to 0. Arrays can be implicitly sized by leaving out the array size declarator and specifying an initialization list. double ratings[ ] = 1.5, 2.0, 2.5, 3.0; char name[ ] = Gadden ; // allocates an array of 7 character elements ( one element is for the NULL terminator ). You cannot use the assignment operator to copy one array s contents to another. int x[3] = 1, 3, 5 ; int y[3] = 2, 4, 6 ; x = y; // NO!, can t do this instead, use: for (int n = 0 ; n < 3 ; n++ ) x[n] = y[n]; The contents of an array may be printed. To do this, use a loop. The only exception is printing a character array that holds a C-string. You can do this using cout; int scores=1,3,2,4; to print: for (int i=0; i<4; i++) cout <<scores[i] << endl; char college[ ] = "Cerritos College"; to print: cout << college << endl; A single element in an array can be passed to a function, or an entire array may be passed to a function. The default passing mechanism for passing an element is by value. Entire arrays are not actually passed, only the address of the array. Array names are passed by reference. function definition: int square ( int x ) void square ( int x [ ], int num ) return x * x; for ( int j = 0 ; j < num ; j++ ) x[j] = x[j] * x[j]; function call: y = square ( NumArray[2] ); square ( NumArray, 10 );

Range-based for Loop C++2011 introduces a range-based for loop to iterate over the elements in a collection ( a c-style array, a vector, an array class object, etc.) const int SIZE = 10; int numbers[size] = 1,2,3,4,5,6,7,8,9,10; Traditional for loop: Range-based for loop: for (int j=0; j < SIZE; j++) for ( auto j : numbers ) // j is type int because numbers is type int cout << j << ; cout << j << ; cout << endl; cout << endl; In the range-based for loop, the parameter j takes on the current value of the array element every iteration of the loop. The range-based for loop can use a reference parameter which allows you to modify the value of the array element using the parameter. To get new values for all of the elements in numbers you can use the following loop: for ( auto& value : numbers ) // value is type reference to an int cout << Enter an integer: ; cin >> value; A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data. double Array [2][3]; // defines an array with 2 rows and 3 columns [ 2-3 element arrays ] Column 0 Column 1 Column 2 Row 0 Array[0][0] Array[0][1] Array[0][2] Row 1 Array[1][0] Array[1][1] Array[1][2] When initializing a two-dimensional array, it is helpful to enclose each row s initialization list in a set of braces. int Hours [3][2] = 8, 5, 7, 9, 6, 3 ; row 0 row 1 row 2 Passing a multi-dimensional array to a function In the function prototype and function header, constant integer size declarators must be specified for all but the first dimension. int ShowArray( int [ ][3], int rows); // prototype for a function that will be passed a 2-dimensional array ShowArray( Sales, 4); // function call that passes ShowArray array Sales, a two dimensional array // with 4 rows and 3 columns.

Searching Chapter 8 - Searching and Sorting Search through an array of elements looking for an exact match. Linear Search Search through an array of sorted or unsorted elements looking for an exact match. If a match is found, return the subscript value for that element, otherwise return 1 to indicate no match was found. If the size of the array is N, the linear search does at most N comparisons. On average, it does N/2 comparisons. The minimum number of comparisons necessary is 1. Binary Search Can only be used on SORTED arrays. If a match is found, return the subscript value for that element, otherwise return 1 to indicate no match was found. Powers of two are used to calculate the maximum number of comparisons the binary search will make on an array of any size. The minimum number of comparisons it will make is 1. To find the maximum number of comparsions that may be made: Find the smallest value of n such that 2 n > the number of elements in the array. If the array size is 25: 2 4 < 25 < 2 5 n = 5, therefore the maximum number of comparisons which may be necessary is 5. Sorting Two sorting algorithms Bubble Sort and Selection Sort were presented. Both are designed to sort the elements in an array in ascending or descending sequence. Bubble Sort is the simplest sort algorithm. Bubble sort performs a series of passes. During each pass, exchanges ( swaps ) are performed when adjacent elements are found to be out of sequence. Passes are repeated until a pass occurs where there are no swaps necessary. Selection Sort is a bit more complex, but is also more efficient. Selection sort only does one exchange per pass. It puts the current value in its correct position in the array. Address operator ( & ) Chapter 9 - Pointers Every variable (or array element) has an address in memory. To retrieve the address of a variable, precede the variable name with the address operator ( & ). int x, int y[5]; &x... returns the address of variable x &y[3]... returns the address of the 4 th element in array y. Note: The address operator is NOT used with an array name when no element is being referenced. y... returns the address of the first element in the array. Equivalent to &y[0]

Pointer variables ( also simply referred to as pointers ) Pointer variables are used to indirectly manipulate the data stored in other variables The ( * ) is used in the declaration of a pointer variable. * is called the indirection operator. int *x, *y; // declares two pointer variables, x and y. The * is necessary to declare these as pointers // and not simply as integer variables. The ( * ) is also used as an operator to dereference a pointer ( indirectly reference the value of the variable the pointer points to ). It is called the indirection operator or dereference operator. int x, y, *xptr, *yptr; // declares two integer variables and two variables that are pointer to integer xptr = &x; yptr = &y; // initializes the values of the pointer variables ( assigns addresses ) int x, y, *xptr = &x, *yptr = &y; // declare and initialize pointer variables in one statement int x = 40, *ptr = &x; cout << ptr << endl; // would print the value of the pointer variable ( an address ) cout << &ptr << endl; // would print the address of the variable ptr cout << *ptr << endl; // would print the value of the variable ptr points to Pointer variables must be declared to point to a specific type of variable (int, double, double, char, etc ) A pointer to one type may not be assigned the address of a variable of a different type. int x; double y; int *ptr = &y; // this is illegal since ptr is a pointer to an integer. Array names may be used as pointers and vice-versa, however, an array name is a constant pointer and cannot be changed. (1) int x[5], *intptr = x; // assigns the address of the 1 st element in array x to intptr. (2) int x[5], *intptr = &x[0]; // this is an equivalent declaration using a subscripted array element Two notations for referring to the value of an array element when there is a pointer to the array. Subscript notation Offset notation ---------------------------------------- ----------------------------------------- x [1] *(x + 1) // add an offset and dereference the array name intptr[1] *(intptr + 1) // add an offset and dereference the pointer When you add a value to a pointer, you are actually adding that value times the size of the data type referenced by the pointer. This is called "pointer arithmetic". Note: The * operator has precedence over the + operator so *x + 1 is different than *(x + 1). Declaring pointers using the keyword const: template: [const] <type> * [const] <identifier>; 4 declarations: int * w; // non-constant pointer to non-constant data of type int. const int * x; //non-constant pointer to constant data of type int. int * const y; //constant pointer to non-constant data of type int. const int * const z; // constant pointer to constant data of type int w is a pointer to an integer x is a pointer to an integer constant y is a consant pointer to an integer z is a constant pointer to an integer constant.

Array names An array name is a constant pointer. An array name cannot be assigned an address of another array. The "value" of an array name is the address of the first element in the array. Given these declarations: int x[10], y[10], *xptr = x, *yptr = y; We could assign different values for the pointers: xptr = y; yptr = x; // change the arrays the pointers point to... We could not do the same operations using the array names!: x = y; // NO! an array name is a constant pointer. Given this declaration: int count[20]; These two expressions are equivalent: count and &count[0] Pointer arithmetic Mathematical operations The following operators may be used with pointer variables: add ( + ), subtract ( - ), add/assign ( += ), subtract/assign ( - = ), increment ( ++ ), decrement (- -) If Ptr is a pointer variable, then expressions such as: Ptr + 3, Ptr 2, Ptr++ are all examples of pointer arithmetic. The result of pointer arithmetic is the calculation of an address. If Ptr was defined as a pointer to int, Ptr +3 would get the address in Ptr and add ( 3 * the size in bytes of an integer ). Initializing Pointers A pointer is designed to point to an object of a specific data type. A pointer may be initialized to the values: 0, nullptr, or to the address of an object of the type the pointer can point to. nullptr is a C++2011 keyword that replaces the use of the named constant NULL. Relational operations may be performed on pointer variables of the same type All of the relational comparisons are valid on pointers of the same type. >, <, >=, <=, = =,!= int x, y, *xptr = &x, *yptr = &y; Examples :xptr = = yptr xptr > yptr xptr >= yptr yptr > xptr yptr!= xptr, etc. Pointers as function arguments. Pointers can be used to pass arguments by reference to a function void double ( int *val ); // prototype for a function that expects to be passed an address Note: The words pointer and address are used as synonyms. The "value" of a pointer is an address We would say this is call by reference using a pointer ( rather than call by reference using a reference ). Given the declaration int x=7; in a function. A call to double using x would be: double ( &x ); Function double must be passed an address to assign to its pointer variable.

void square ( int & ); // prototype for a function that gets an argument passed by reference using a reference // given x is an integer variable in a calling function, the function call to pass x would be square(x); void square ( int * ); // prototype for a function that gets an argument passed by reference using a pointer // given x is an integer variable in a calling function, the function call to pass x would be square(&x); Dynamic memory allocation For a function that declares an array, the size declarator must be an integer constant expression. The compiler must be able to determine (at compile time) how much memory must be allocated at runtime to hold the array. When it is not known how many elements storage is needed for and there could be potentially a sizeable difference between different executions of a program, memory can be allocated dynamically. This allows for the allocation of an arbitrary amount of memory at runtime. Memory can also be allocated for a single element. The new and delete operators are used to allocate and release dynamic memory. These operators require the use of pointers. Memory is dynamically allocated using the new operator and a pointer variable. int *p = new int; // dynamically allocate space for a single integer int *p = new int[100]; // dynamically allocate space for 100 integers Both of these declarations assign to p the address of the first ( or only ) element. int *p = new int[count]; Dynamically allocate space for count elements. Count does NOT have to be declared as a named constant. Also, the value of count does NOT need to be known at compile time! cout << "For how many students do you have test scores? "; cin >> count; int *p = new int[count]; delete p; delete [ ] p; // release the memory previously dynamically allocated for a single element // release the memory previously dynamically allocated for a group of elements In a declaration such as p[0], p[10], p[99], etc. int *p = new int[100]; p can be used to refer to the elements using subscript notation. Class unique_ptr #include <memory> preprocessor directive required C++2011 template class used to declare a smart pointer Used with dynamically allocated memory Automatically releases memory when the block it is created in is exited Use of the delete keyword is not required and cannot be used for a unique pointer unique_ptr< int[ ] > ptr ( new int[ 10 ] ); //dynamically allocated array of 10 integers all initialized to 0 for (int index=0; index < 10; index++) cout << Enter an integer number: ; cin >> ptr[index]; //can use familiar [ ] notation with the pointer name ptr