Let’s Look at Some Functions Boolean Functions – functions that return ‘true’ or ‘false’ Other Functions – some with arguments, some with none.

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 7: Arrays In this chapter, you will learn about
Arrays.
An introduction to pointers in c
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
Programming and Data Structure
Chapter 7: User-Defined Functions II
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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 8 Arrays and Strings
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
(continue) © by Pearson Education, Inc. All Rights Reserved.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Learners Support Publications Classes and Objects.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CS 261 – Data Structures Introduction to C Programming.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CHAPTER 7: Arrays CSEB113 PRINCIPLES of PROGRAMMING CSEB134 PROGRAMMING I by Badariah Solemon 1BS (Feb 2012)
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
1 Multidimensional Arrays Chapter 13 2 The plural of mongoose starts with a "p" Initializing a multidimensional array Processing by.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Test 2 Review Outline.
User-Written Functions
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Hassan Khosravi / Geoffrey Tien
Pointers and Pointer-Based Strings
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Submitted By : Veenu Saini Lecturer (IT)
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Presentation transcript:

Let’s Look at Some Functions Boolean Functions – functions that return ‘true’ or ‘false’ Other Functions – some with arguments, some with none

Arrays as Function Arguments If you have a function that requires an array as its argument, you must indicated to the function (and the compiler) that it is an array. void GetData(int Data[], int size) { body of GetData } The [] indicates that an array is being passed to the function. When an array is passed to a function THE ADDRESS of the array is what is actually passed. Size is necessary so the function will know how many cells the array being passed contains.

Passing an Array to a Function When you pass an array to a function you include only the name of the array. The size of the array is usually passed as an additional argument. Example: const int CLASS_SIZE=30; int grades[CLASS_SIZE]; GetData(grades, CLASS_SIZE); Note: To pass only one element of an array you must specify the element. This is a pass by value just like any other variable. Example: DisplayHighest(grades[high_grade]);

Pass by Reference When the address of a variable is passed to a function instead of the value of the variable it is called Pass by Reference Functions can change the value of arguments that are passed by reference since the address of the variable is what is passed.

Why Array Size is Not Required in Function Definition (but is usually provided in a second argument) Note: Since arrays are ‘passed by reference’, only the address of the beginning of the array is sent to a function. Since the function knows its data type it can calculate the location of the individual cells. The function can merrily calculate off the end of data storage if the actual size is not passed (or is a global constant) and array bounds are not checked by the function.

A Visual Look at ‘Pass by Reference’ int numbers[3] = {10,30,20}; //sizeof(int) is 4 bytes Memory: x = Biggest(numbers, 3); ************************************************* Address (numbers) Value (numbers) int Biggest(int nums[ ], int size) { int largest; largest = nums[0]; for (int i=1; i < size; i++) if (nums[i] > largest) largest = nums[i] return largest; } What is passed? Biggest(100234, 3) Memorynumssizelargestbiggest Address Value ,3030

The Address of a Variable There is an operator is C++ that returns the address of its operand. This is only being introduced so that we can physically look at how memory storage works and better understand how arrays are passed to functions. Note: Your book refers to a section called Reference Variables which look VERY similar. It is not the same thing though it uses the same symbol (&). We will not be covering Reference Variables in this course nor will you be responsible for the ‘address-of’ operator, the ‘&’.

Summary: Arrays as Function Arguments Are a pass by reference (rather than value). The physical memory address of the start of the array is passed rather than the entire array. The function calculates the location of specific elements using the size of the data type of the array You should usually pass the size of the array to the function as well and your function should enforce boundary conditions Unlike other arguments we previously used, the function being passed the array can alter the contents of the array. To pass the array simply use its name To indicate that an argument in a function definition is an array you need to add [] after the argument name.

const Array Parameters (or Arguments) If a function merely needs to look at the elements in an array but its task does not require that it change the array you can declare the argument type as const. Like other const variables, its value cannot be changed in execution and the function will be unable to change the contents of the array. Syntax: return_type function_name(const type arg_name, … )

More Two-dimensional Arrays Example: int numbers[3][4]; Concept: Remember how 2-dimensional arrays are stored in memory. A two-dimensional array can be thought of as an array of arrays. We used this concept for arrays of C- strings. Array 0 Array 1 Array 2 Numbers

Passing a Two-Dimensional Array to a Function When a two-dimensional array is used as a function argument you MUST specifically declare the number of columns in the definition so the compiler can calculate the start of each row. This can be done with a literal, named integer constant, or global constant. Since C/C++ does no bounds checking it does not need the number of rows (HOWEVER – your function will need it so it can perform the boundary conditions check.)

Example: Function with 2D Array Example function header: void MyFunction(float twoD_array[][20], int size) Note: The number of columns is declared explicitly ************** Example function prototype: void MyFunction(float [][20], int); Note: The number of columns must still be specified. ************** Example function call: MyFunction(my_array, SIZE); Note: Only the name is included in the call. Remember that the address of myarray is what is being passed.