Functions, Arrays & Structs

Similar documents
Functions, Arrays & Structs

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

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

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

! The address operator (&) returns the address of a. ! Pointer: a variable that stores the address of another

Data Types (C/C++) Structures

Ch 11. Structured Data (11.2 to 11.8) Data Types Structures. Data Types (C/C++) Data Type: CS 2308 Fall example: Integer.

Structures. Data Types Structures. Data Types (C/C++) Gaddis: A Data Type consists of: Unit 7. example: Integer. CS 1428 Spring 2018

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Week 4. Pointers and Addresses. Dereferencing and initializing. Pointers as Function Parameters. Pointers & Structs. Gaddis: Chapters 9, 11

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

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

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

Chapter 7: Arrays Copyrig opy ht rig 2012 Pea e rson a Educa Educ ti a on, Inc I. nc

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

CS 115 Midterm 2 Solutions

Functions and Recursion

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

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. Array Terminology. Array Terminology

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Arrays. Arizona State University 1

7 Arrays. 7.1 Arrays Hold Multiple Values TOPICS. CONCEPT: An array allows you to store and work with multiple values of the same data type.

Array Part dimensional arrays - 2-dimensional array operations - Arrays of Strings - N-dimensional arrays

Ch 6. Functions. Example: function calls function

Name Section: M/W or T/TH. True or False (14 Points)

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

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

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

BITG 1113: Function (Part 2) LECTURE 5

Array Elements as Function Parameters

Using Parallel Arrays. Parallel Array Example

Topics. Functions. Functions

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Week 3: File I/O and Formatting 3.7 Formatting Output

CSCE Practice Midterm. Data Types

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

True or False (12 Points)

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Object Oriented Design

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Integer Data Types. Data Type. Data Types. int, short int, long int

Question 1 Consider the following structure used to keep employee records:

True or False (15 Points)

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

Functions that Return a Value. Approximate completion time Pre-lab Reading Assignment 20 min. 92

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

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

Exam 3 Chapters 7 & 9

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

BITG 1113: Array (Part 1) LECTURE 8

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

Name SECTION: 12:45 2:20. True or False (12 Points)

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

C++ PROGRAMMING SKILLS Part 4: Arrays

Parameter Passing Mechanisms. Reference Parameters

CS3157: Advanced Programming. Outline

! A pointer variable (or pointer): ! An asterisk is used to define a pointer variable. ! ptr is a pointer to an int or

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

CSC1322 Object-Oriented Programming Concepts

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Variable Definitions and Scope

Pointers, Dynamic Data, and Reference Types

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

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

2. Functions I: Passing by Value

Arrays. Week 4. Assylbek Jumagaliyev

True or False (14 Points)

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.

Input And Output of C++

Fundamentals of Programming Session 24

Here, type declares the base type of the array, which is the type of each element in the array size defines how many elements the array will hold

BITG 1233: Introduction to C++

Homework #3 CS2255 Fall 2012

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

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

7/21/10 MOTIVATION. Did you see Inception this weekend? COMP 10 EXPLORING COMPUTER SCIENCE. Lecture 6 Arrays

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

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

Interview Questions of C++

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

C++ For Science and Engineering Lecture 12

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

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

by Pearson Education, Inc. All Rights Reserved. 2

CS 376b Computer Vision

Introduction to C++ (Extensions to C)

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

BITG 1113: Array (Part 2) LECTURE 9

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

Transcription:

Functions, Arrays & Structs Unit 1 Chapters 6-7, 11 Function Definitions l Function definition pattern: datatype identifier (parameter1, parameter2,...) { statements... Where a parameter is: datatype identifier CS 2308 Fall 2018 Jill Seaman 1 datatype: the type of data returned by the function. identifier: the name by which it is possible to call the function. parameters: Like a regular variable declaration, act within the function as a regular local variable. Allow passing arguments to the function when it is called. statements: the function's body, executed when called. 2 Function Call, Return Statement l Function call expression Causes control flow to enter body of function named identifier. parameter1 is initialized to the value of expression1, and so on for each parameter expression1 is called an argument. l Return statement: identifier ( expression1,... ) inside a function, causes function to stop, return control to caller. l The value of the return expression becomes 3 the value of the function call return expression; Example: Function // function example #include <iostream> using namespace std; int addition (int a, int b) { int result; result=a+b; return result; int main () { int z; z = addition (5,3); cout << "The result is " << z <<endl; l What are the parameters? arguments? l What is the value of: addition (5,3)? l What is the output? 4

Void function l A function that returns no value: void printaddition (int a, int b) { int result; result=a+b; cout << the answer is: << result << endl; use void as the return type. l the function call is now a statement (it does not have a value) int main () { printaddition (5,3); 5 Prototypes l In a program, function definitions must occur before any calls to that function l To override this requirement, place a prototype of the function before the call. l The pattern for a prototype: datatype identifier (type1, type2,...); the function header without the body (parameter names are optional). 6 Arguments passed by value Example: Pass by Value l Pass by value: when an argument is passed to a function, its value is copied into the parameter. l It is implemented using variable initialization (behind the scenes): int param = argument; l Changes to the parameter in the function body do not affect the value of the argument in the call l The parameter and the argument are stored in separate variables; separate locations in 7 memory. #include <iostream> l using namespace std; void changeme(int); int main() { int number = 12; cout << "number is " << number << endl; changeme(number); cout << "Back in main, number is " << number << endl; return 0; int myvalue = number; void changeme(int myvalue) { myvalue = 200; cout << "myvalue is " << myvalue << endl; changeme failed to change the argument! Output: number is 12 myvalue is 200 Back in main, number is 12 8

Parameter passing by Reference Example: Pass by Reference l Pass by reference: when an argument is passed to a function, the function has direct access to the original argument (no copying). l Pass by reference in C++ is implemented using a reference parameter, which has an ampersand (&) in front of it: void changeme (int &myvalue); l A reference parameter acts as an alias to its argument, it is NOT a separate storage location. l Changes to the parameter in the function DO 9 affect the value of the argument #include <iostream> l using namespace std; void changeme(int &); int main() { int number = 12; cout << "number is " << number << endl; changeme(number); cout << "Back in main, number is " << number << endl; return 0; myvalue is an alias for number, only one shared variable void changeme(int &myvalue) { myvalue = 200; cout << "myvalue is " << myvalue << endl; Output: number is 12 myvalue is 200 Back in main, number is 200 10 Scope of variables More scope rules l For a given variable definition, in which part of the program can it be accessed? Global variable (defined outside of all functions): can be accessed anywhere, after its definition. Local variable (defined inside of a function): can be accessed inside the block in which it is defined, after its definition. Parameter: can be accessed anywhere inside of its function body. l Variables are destroyed at the end of their l Variables in the same exact scope cannot have the same name - Parameters and local function variables cannot have the same name - Variable defined in inner block can hide a variable with the same name in an outer block. int x = 10; if (x < 100) { int x = 30; cout << x << endl; cout << x << endl; scope. 11 12 Output: l Variables defined in one function cannot be seen from another. 30 10

Overloaded Functions l Overloaded functions have the same name but different parameter lists. l The parameter lists of each overloaded function must have different types and/or number of parameters. l Compiler will determine which version of the function to call by matching arguments to parameter lists 13 l Example: Overloaded functions double calcweeklypay (int hours, double payrate) { return hours * payrate; double calcweeklypay (double annsalary) { return annsalary / 52; int main () { int h; double r; Output: Enter hours worked and pay rate: 37 19.5 Pay is: 721.5 Enter annual salary: 75000 Pay is: 1442.31 cout << "Enter hours worked and pay rate: "; cin >> h >> r; cout << "Pay is: " << calcweeklypay(h,r) << endl; cout << "Enter annual salary: "; cin >> r; cout << "Pay is: " << calcweeklypay(r) << endl; return 0; 14 Default Arguments Example: Default Arguments l A default argument for a parameter is a value assigned to the parameter when an argument is not provided for it in the function call. l The default argument patterns: in the prototype: datatype identifier (type1 = c1, type2 = c2,...); OR in the function header: datatype identifier (type1 p1 = c1, type2 p2 = c2,...) {... l c1, c2 are constants (named or literals) 15 void showarea (double length = 20.0, double width = 10.0) { double area = length * width; cout << The area is << area << endl; l This function can be called as follows: showarea(); ==> uses 20.0 and 10.0 The area is 200 showarea(5.5,2.0); ==> uses 5.5 and 2.0 The area is 11 showarea(12.0); ==> uses 12.0 and 10.0 The area is 120 16

l An array is: Arrays - A series of elements of the same type - placed in contiguous memory locations - that can be individually referenced by using an index along with the array name. l To declare an array: datatype identifier [size]; int numbers[5]; - datatype is the type of the elements - identifier is the name of the array - size is the number of elements (constant) 17 Array initialization l To specify contents of the array in the definition: float scores[3] = {86.5, 92.1, 77.5; - creates an array of size 3 containing the specified values. float scores[10] = {86.5, 92.1, 77.5; - creates an array containing the specified values followed by 7 zeros (partial initialization). float scores[] = {86.5, 92.1, 77.5; - creates an array of size 3 containing the specified values (size is determined from list). 18 Array access l to access the value of any of the elements of the array individually, as if it was a normal variable: scores[2] = 89.5; - scores[2] is a variable of type float l rules about subscripts (aka indexes): - they always start at 0, last subscript is size-1 - the subscript must have type int - they can be any expression l watchout: brackets used both to declare the array 19 and to access elements. Arrays: operations l Valid operations over entire arrays: function call: myfunc(scores,x); l Invalid operations over entire arrays: assignment: array1 = array2; comparison: array1 == array2 output: cout << array1; input: cin >> array2; Must do these element by element, probably using a for loop 20

Processing arrays l Assignment: copy one array to another const int SIZE = 4; int oldvalues[size] = {10, 100, 200, 300; int newvalues[size]; for (int count = 0; count < SIZE; count++) newvalues[count] = oldvalues[count]; Example: Processing arrays Computing the average of an array of scores: const int NUM_SCORES = 8; int scores[num_scores]; cout << Enter the << NUM_SCORES << programming assignment scores: << endl; for (int i=0; i < NUM_SCORES; i++) { cin >> scores[i]; l Output: displaying the contents of an array const int SIZE = 5; int numbers[size] = {10, 20, 30, 40, 50; for (int count = 0; count < SIZE; count++) int total = 0; //initialize accumulator for (int i=0; i < NUM_SCORES; i++) { total = total + scores[i]; double average = static_cast<double>(total) / NUM_SCORES; cout << numbers[count] << endl; 21 22 Finding highest and lowest values in arrays l Maximum: Need to track the highest value seen so far. Start with highest = first element. const int SIZE = 5; int array[size] = {10, 100, 200, 30; int highest = array[0]; for (int count = 1; count < SIZE; count++) if (array[count] > highest) highest = array[count]; cout << The maximum value is << highest << endl; Arrays as parameters l In the function definition, the parameter type is a variable name with an empty set of brackets: [ ] - Do NOT give a size for the array inside [ ] void showarray(int values[], int size) l In the prototype, empty brackets go after the element datatype. void showarray(int[], int) l In the function call, use the variable name for the array. showarray(numbers, 5) 23 l An array is always passed by reference. 24

Two-Dimensional Arrays Two-Dimensional Array processing l Like a table in a spreadsheet: rows and columns l Declaration requires two size declarators: int table [5][3]; // 5 rows, 3 columns l Rows are always first l 2D arrays can be initialized: int table [2][3] = { {1, 2, 3, {4, 5, 6 ; 1 2 3 4 5 6 25 l Access an element of the array using two indices: int table [2][3] = { {1, 2, 3, {4, 5, 6 ; cout << table[0][2]; Output: 3 l Two dimensional arrays can be passed to functions. l The number of columns is required in the parameter declaration: void showtable (int array[][3], int rows) { 26 Two-Dimensional Array functions Structures l 2D array processing usually requires nested for loops: void showtable (int array[][3], int rows) { for (int x=0; x<rows; x++) { for (int y=0; y<3; y++) cout << setw(4) << array[x][y] << ; cout << endl; l How showtable is called: int table [2][3] = { {1, 2, 3, {4, 5, 6 ; showtable(table,2); 28 27 l A structure stores a collection of objects of various types l Each element in the structure is a member, and is accessed using the dot member operator. struct Student { int idnumber; string name; int age; string major; ; Defines a new data type Student student1, student2; Defines new variables student1.name = John Smith ; Student student3 = {123456, Ann Page,22, Math ;

Structures: operations Arrays of Structures l Valid operations over entire structs: l You can store values of structure types in arrays. assignment: student1 = student2; function call: myfunc(gradstudent,x); void myfunc(student, int); //prototype Student roster[40]; //holds 40 Student structs l Each student is accessible via the subscript notation. l Invalid operations over structs: comparison: student1 == student2 output: cout << student1; input: cin >> student2; Must do these member by member 29 roster[0] = student1; l Members of structure accessible via dot notation cout << roster[0].name << endl; 30 Arrays of Structures: initialization Arrays of Structures l To initialize an array of structs: l Arrays of structures processed in loops: struct Student { int idnumber; string name; int age; string major; ; int main() { Student roster[] = { {123456,"Ann Page",22,"Math", {111222,"Jack Spade",18,"Physics" ; Student roster[40]; //input for (int i=0; i<40; i++) { cout << "Enter the name, age, idnumber and " << "major of the next student: \n"; cin >> roster[i].name >> roster[i].age >> roster[i].idnumber >> roster[i].major; //output all the id numbers and names for (int i=0; i<40; i++) { cout << roster[i].idnumber << endl; cout << roster[i].name << endl; 31 32