Lec 14 Oct 23, 02.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Functions Prototypes, parameter passing, return values, activation frams.
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Short C++ Review CS 302 – Data Structures. Call by value/reference.
Arrays CSE 5100 Data Structures and Algorithms. One-Dimensional Arrays  A list of values with the same data type that are stored using a single group.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Functions Pass by Value Pass by Reference IC 210.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Upcoming Events Project Check Due Next Week in Lab –Phase I –main(), menu() Functions and Reading in File –Stub Functions Last Lectures Next Week –Project.
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.
Arrays CS 308 – Data Structures. One-Dimensional Arrays A list of values with the same data type that are stored using a single group name (array name).
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Pointers CSE 5100 Data Structures and Algorithms.
計算機程式語言 Lecture 07-1 國立臺灣大學生物機電系 7 7 Arrays.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
A First Book of ANSI C Fourth Edition Chapter 8 Arrays.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Copyright 2003 Scott/Jones Publishing Alternate Version of Starting Out with C++, Third Edition Chapter 8 Arrays.
CSCI 161 Lecture 14 Martin van Bommel. New Structure Recall “average.cpp” program –Read in a list of numbers –Count them and sum them up –Calculate the.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
C++ Review Data Structures.
Alternate Version of Starting Out with C++, Third Edition
Fucntions in C++ Malik Jahan Khan
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Standard Version of Starting Out with C++, 4th Edition
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.
Array An “average.cpp” program
Pointers and Pointer-Based Strings
CNG 140 C Programming (Lecture set 10)
CS 1430: Programming in C++.
Chapter 9: Pointers.
FUNCTIONS WITH ARGUMENTS
Chapter 8: Collections: Arrays
Chapter 5 Function Basics
Chapter 9: Pointers.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Value returning Functions
CS150 Introduction to Computer Science 1
Lecture 12 Oct 16, 02.
Functions with arrays.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
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.
Pointers and dynamic objects
Standard Version of Starting Out with C++, 4th Edition
Structure (i.e. struct) An structure creates a user defined data type
Presentation transcript:

Lec 14 Oct 23, 02

Arrays as Arguments Individual array elements are passed to a called function in the same manner as individual scalar variables. eg: find_min(volts[2], volts[6]); passes the values of the elements volts[2] and volts[6] to the function find_min.

Passing whole array as argument Passing a complete array of values to a function is in many respects an easier operation than passing individual elements. The called function receives access to the actual array, rather than copy of the values in the array. Eg: find_max(volts); makes the complete volts array available to the find_max function

Other examples // Array declarations int nums[5]; char keys[256]; double volts[500], current[500]; // function calls find_max(nums) find_ch(keys) calc_tot(nums, volts, current) // function headers int find_max(int vals[5]) char find_ch(char in_keys[256]) void calc_tot( int arr1[5], double arr2[500], double arr3[500] ) / * The parameter list in the function header still refer to the original array created outside the function. */

#include<iostream.h> Example 1 #include<iostream.h> const int MAXELS = 5; int find_max(int [MAXELS]);// function prototype declaration int main() { int nums[MAXELS] = {2, 18, 1, 27, 16}; cout<< “The maximum is “<<find_max(nums) <<endl; return 0; }

int find_max(int vals[MAXELS]) Example 1 function int find_max(int vals[MAXELS]) { int i, max = vals[0]; for (i=1; i<MAXELS; i++) if (max<vals[i] ) max = vals[i]; return max; }

Alternative function header int find_max( int vals[]) instead of int find_max(int vals[MAXELS]);

Example 2 #include<iostream.h> int find_max(int [], int); //function prototype int main() { const int MAXELS = 5; int nums[MAXELS] = {2, 18, 1, 27, 16}; cout<<“ The maximum value is “ <<find_max(nums, MAXELS) <<endl; return 0; }

int find_max(int vals[], int num_els) Example 2 function int find_max(int vals[], int num_els) { int i, max = vals[0]; for(i=1; i< num_els; i++) if (max<vals[i]) max = vals[i]; return max; }

2-D arrays int test[7][9]; // array declaration float factors[26][10]; double thrusts[256][52]; find_max(test); // function calls obtain(factors); average(thrusts); int find_max(int nums[7][9]) // function header int obtain(float values[26][10]) int average(double vals[256][52])

Alternative function header for 2-D arrays display(int nums[][4]);