Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Kernighan/Ritchie: Kelley/Pohl:
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Topic 8 – Introduction to Pointers and Function Output Parameters.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
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 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Review of C++ Programming Part II Sheng-Fang Huang.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Array (continue).
(continue) © by Pearson Education, Inc. All Rights Reserved.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
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.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Variables and memory addresses
Arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
ICS103: Programming in C 7: Arrays Muhamed F. Mudawar.
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];
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
User-Defined Functions (cont’d) - Reference Parameters.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
User-Written Functions
C Arrays.
Lecture 18 Arrays and Pointer Arithmetic
Pointers Call-by-Reference CSCI 230
Arrays Arrays A few types Structures of related data items
Presentation transcript:

Topic 9A – Arrays as Function Arguments

CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an array element as a function argument, i.e. passing one integer from an array of integers into a function. Use a full array as a function argument, i.e. passing an entire array into a function.

CISC105 – Topic 9A Array Elements as Function Arguments Individual array elements can be used as function arguments in the same way their corresponding data type “simple” variables can be. If a function takes one integer as an parameter, an element of a integer array can be used.

CISC105 – Topic 9A Array Elements as Function Arguments int x[5] = { 1, 2, 3, 4, 5 }; float average; average = avg_of5(x[0],x[1],x[2],x[3],x[4]); printf(“The total is %f.”,average);... float function_y(int a, int b, int c, int d, int e) { return (a + b + c + d + e) / 5.0; }

CISC105 – Topic 9A Array Elements as Function Arguments Notice that there is no difference between passing a “simple” integer into such a function and passing an element of an integer array into such a function. We can also use array elements as function output parameters. We simply pass in the address of the array element, in the same way as we do with “simple” variables, using the “&” operator.

CISC105 – Topic 9A Array Elements as Function Arguments int x[6] = { 1, 2, 3, 4, 5, 0}; tot_of5(x[0], x[1], x[2], x[3], x[4], &x[5]); printf(“The total is %d.”,x[5]);... void tot_of5(int a, int b, int c, int d, int e, int *p_f) { *p_f = a + b + c + d + e; }

CISC105 – Topic 9A Arrays as Function Arguments In addition to using array elements as function arguments, an entire array can be used as a function argument. However, this is a somewhat more complicated process. The first point to realize is that an array name (without an index) evaluates to the memory address of the first element of the array. Thus, if integer_array is an array of integers: integer_array == &integer_array[0]

CISC105 – Topic 9A Arrays as Function Arguments As the array name is equal to the address of the first element of the array, we can think of this as a pointer to the array. Now…how do we pass arrays as function arguments? To pass an array of variables, the parameter name is followed by a “[]”. Notice that the size of the array is NOT placed inside the brackets.

CISC105 – Topic 9A Arrays as Function Arguments Therefore, for a function that takes two arrays, one of floats and one of integers, as parameters, the prototype would look like: void function1(float [], int []);  The function definition would look like: void function1(float x[], int y[]) { }

CISC105 – Topic 9A Arrays as Function Arguments Notice that the size of the array is not passed into the function. So, this information must be passed in some other way. A program to set all elements of an integer array to some value, could look like: void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

CISC105 – Topic 9A Arrays as Function Arguments This function takes an array, the size of the array, and the value to set the array elements to as parameters. Thus, to set all 10 elements of the array y to 27, we could call the function: fill_array(y,10,27); void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

CISC105 – Topic 9A Arrays as Function Arguments Remember that the array name is simply the address of the first element of that array. Thus, we could also call the function: fill_array(&y[0],10,27) ; void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

CISC105 – Topic 9A Arrays as Function Arguments Therefore, for a function such as: void fill_array(int [], int, int); We could write a program: int y[10]; fill_array(y, 10, 27); fill_array(&y[0], 10, 27);... if (y == &y[0]) printf(“They are the same!”); These two statements do the exact same thing as y = &y[0]. This is just another demonstration that y and &y[0] are the same.

CISC105 – Topic 9A Array Name and Pointer Equivalence Again, the array name is simply the address of the first element of that array, thus a pointer to the array. Therefore, void fill_array(int [], int, int); Could rewritten as: void fill_array(int *, int, int);

CISC105 – Topic 9A Array Name and Pointer Equivalence Likewise, we rewrite the function, using pointer notation: void fill_array( int *array, int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

CISC105 – Topic 9A Array Name and Pointer Equivalence Therefore, these two functions are equivalent: void fill_array( int *array, int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} } void fill_array( int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) {array[i] = set_value;} }

CISC105 – Topic 9A Array Name and Pointer Equivalence So, which way is better? Well…the second form does not specify weather array is an array of integers, or simply an integer function output parameter. That, as well as being more confusing, show why the first form is preferred. void fill_array( int *array, int n, int set_value ) void fill_array( int array[], int n, int set_value )

CISC105 – Topic 9A Arrays are Call-by-Reference OK, so now we know how to pass an array into a function. Notice, however, that when we pass an array, we are simply passing the memory address of the first element. Therefore, arrays are always passed call-by- reference, meaning that if the function we pass an array to modifies that array, that change also shows in the calling function after the return, just like using a function output parameter.

CISC105 – Topic 9A Arrays are Call-by-Reference int x[5] = { 0, 0, 0, 0, 0 }; printf(“%d %d %d %d %d\n”,x[0],x[1],x[2],x[3],x[4]); fill_array(x,5,27); printf(“%d %d %d %d %d\n”,x[0],x[1],x[2],x[3],x[4]);... void fill_array(int array[], int n, int set_value) { int i; for (i = 0; i < n; i++) array[i] = set_value; } Program Output:

CISC105 – Topic 9A Arrays as Strictly Function Input Parameters Sometimes, we want an array to be passed into a function, yet, not allow the function to change the array. This is possible by using the C keyword, const (standing for constant). This keyword is placed before the datatype and tells the C language not to let the function modify the variable (or the array, in this case).

CISC105 – Topic 9A Arrays as Strictly Function Input Parameters Thus, a function to compute the sum of an array, and return it, would look like: int array1[5] = { 1, 2, 3, 4, 5 }; int sum, array1_size=5; sum = array_total(array1, array1_size);... int array_total(const int x[], int size) { int counter, total = 0; for (count = 0; count < size; count++) total += x[count]; return total; }

CISC105 – Topic 9A Arrays as Strictly Function Input Parameters Notice in this program that the array (named x in the function and named array1 in the main program) cannot be modified by the function as it has been declared as a constant. Also keep in mind that this is the SAME array, as arrays are passed call-by- reference. Also note that the size, an integer, is call-by- value by default; thus changing size in the function cannot change array1_size in the main program.

CISC105 – Topic 9A The const Keyword The const keyword will not allow the function to change the array. If the function attempts to change any element of the array, a compiler error will be generated and the program will not compile. This keyword can also be used with variables, in addition to arrays. It has the same meaning; if the function attempts to change the variable, a compiler error will be generated and the program will not compile.

CISC105 – Topic 9A The const Keyword Thus, if the function were written like this, not only would a change to size in the function not affect array1_size, but we cannot even change size inside the function. sum = array_total(array1, array1_size);... int array_total(const int x[], const int size) { int counter, total = 0; for (count = 0; count < size; count++) total += x[count]; return total; }

CISC105 – Topic 9A Summary: Arrays as Function Input An array element can be passed into a function just like a normal “simple” variable. This is call-by- value (there are two copies, one for the caller and for for the called function.) An array can be passed into a function. This is call-by-reference (any change to the array in the called function changes the array in the calling function). In order to make an array passed into a function input-only (the called function cannot change the array), preface the parameter list (in the prototype and function definition) with the C keyword const.

CISC105 – Topic 9A Arrays as Function Output Now we have seen how to pass an array into a function. So, how do we return an array from a function? In the C programming language, it is not legal to specify an array as a function return type. Therefore, in order for a function to have an array as an output, we must use a function output parameter.

CISC105 – Topic 9A Arrays as Function Output Suppose we wish to write a function that takes two arrays as input and an array as output. This function adds the first elements of the two input arrays and puts the result into the first element of the output array, the second elements of the input arrays and puts the result into the second element of the output array, etc…

CISC105 – Topic 9A Arrays as Function Output Thus, we need the following parameters for this function: const int input1[], const int input2[], int output[], int n For this example, we are assuming all of the arrays are the same size. Note that the parameters do not include the size of the arrays, so we must pass the size separately, in this case, in the variable n.

CISC105 – Topic 9A Arrays as Function Output This function may look like: void add_arrays(const int input1[], const int input2[], int output[], int n) { int count; for (count = 0; count < n; count++) output[count] = input1[count] + input2[count]; return; }

CISC105 – Topic 9A Arrays as Function Output To call this function, we would write: int array1[5] = { 1, 2, 3, 4, 5 }; int array2[5] = { 6, 7, 8, 9, 10 }; int array3[5]; int i; add_arrays(array1, array2, array3, 5); for (i=0; i<5; i++) printf(“%d ”,array3[i]); Program Output:

CISC105 – Topic 9A Arrays as Function Output Notice that when we pass an array to a function, we do not pass the size of the array. Thus, we need to remember to ensure that the calling function declares the array to be a certain size and both the calling function and the called function should not overstep the bounds on that array. When an array is passed into a function, especially if the array is going to be used for output (the function is going to put values in that array), the size of that array must be passed to the function as well.

CISC105 – Topic 9A Summary An array can be passed into a function, as an input parameter, or an output parameter. To do so, the bracket set “[]” is used, in the function prototype and in the function definition, to indicate an array. An array name evaluates to the memory address of the first element of the array. As such, passing an array to a function is always call-by-reference, which allows a function to modify the actual array.

CISC105 – Topic 9A Summary In order to not allow a function to modify the array, the C keyword const can be used. This declares the array to be a constant, and any attempt to modify that array causes an error. When an array is passed to a function, the size of that array is not implicitly known. Therefore, we must pass the size of the array as a separate, explicit array.