Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Multiple-Subscripted Array
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Arrays- Part 2 Spring 2013Programming and Data Structure1.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Copyright © 2002 W. A. Tucker1 Chapter 9 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Structuring Data: Arrays ANSI-C. Representing multiple homogenous data Problem: Input: Desired output:
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Functions BICSE-6A Mr. Naeem Khalid Lecturer, Dept. of Computing.
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];
CSIS 113A Lecture 10 Arrays Glenn Stevenson CSIS 113A MSJC.
User-Defined Functions (cont’d) - Reference Parameters.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Introduction to programming in java Lecture 21 Arrays – Part 1.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Passing Objects to Methods
EGR 2261 Unit 10 Two-dimensional Arrays
Arrays & Functions Lesson xx
One-Dimensional Array Introduction Lesson xx
Review for Final Exam.
CS150 Introduction to Computer Science 1
Review for Final Exam.
CS149D Elements of Computer Science
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and int max(int x, int y, int z) are different functions, as are int hash(int x) and int hash(char c). By convention functions with the same name do similar work.

Pitfalls Having two functions with the same name and number and types of arguments but different return types is an error. Since argument types may be converted to compatible types when a function is called, you must be careful when two functions have compatible types.

Example Consider the two functions: int foo(int x, int y) { return x < y ? x : y; } double foo(double x, double y) { return x – y; } foo(2, 3) calls the first function, foo(2.0, 3.0) calls the second function, as does foo(2, 3.0).

Default Values The last parameters in a function may be given default values if they are call-by-value parameters. The syntax is: int foo(int a, int b = 10) { } If foo is called with two arguments, then a and b are both set to the corresponding values. If called with one value, then a is set, and b is set to 10.

Review of Functions Functions may have a return type and can be used in expressions, or have return type void and are called as statements. Functions may have call-by-value and call-by- reference parameters. The former are used for input parameters only. A function with a return type must have a return statement to pass back a value.

Review of Functions (cont'd) Functions are used in program design to implement steps as the programmer breaks the problem down into pieces. Functions may be overloaded, i.e., two functions may have the same name. Functions may have default values.

Problem Suppose we want to read in five ints and then print them back out, only in the reverse order in which they are read. No problem: int a1, a2, a3, a4, a5; cin >> a1 >> a2 >> a3 >> a4 >> a5; cout << a5 << “ “ << a4 << “ “ << a3 << “ “ << a2 << “ “ << a1 << endl;

Another Problem Now suppose we want to read in 100 ints (or 1000 ints) and print them back out in the reverse order read in. Problem, or at least, boring.

A Solution Instead of using five, or 100, or 1,000 variables to store the input, it would be better to use just one variable that has five, or 100, or 1,000 parts. We can do this in C++ by using an array. An array variable is a single variable, but can hold multiple items (all of which may be the same type). The different items are referred to by number. Picture a box with numbered compartments.

a

Array Syntax Instead of writing a1, a2, a3,..., we write a[1], a[2], a[3],.... (read a-sub-1, a-sub-2, a-sub3). The “ a ” refers to the entire box, and the subscript refers to the particular item in the box. The advantage of this notation is that the subscript can be a variable, or even an expression, e.g., a[i].

Solution Revisited So, if we want to read in 100 numbers and print them out backwards, we can use an array to store the values, and two loops – one to read in the numbers and one to print them out. Problem solved for(int i = 0; i < 100; i++) cin >> a[i]; for(int i=99; i >= 0; i—) cout << a[i] << “ “; cout << endl;

Arrays An array is a data object that can hold a number of homogeneous values (all of the same type) The elements of a particular array are numbered, starting at 0. An array is a single variable, that is, the identified is associated with the big box. Arrays must be declared before they are used, and the declaration includes the size: int a[100];

Example Problem: Read in 20 grades (from 0 to 100). Calculate and print out the average grade and the standard deviation. The formula for the standard deviation is sqrt( ( ∑ i=1,n ( grade i – average) 2 ) /20). We will need two loops: one to read in the grades and compute the average, and one to compute the sum necessary for the standard deviation. Since we can only read in the grades once, we must save their values in an array.

A Solution int grades[20], sum = 0; for(int i=0; i<20; i++) { cin >> grades[i]; sum += grades[i]; } double ave = ((double)sum)/20; double sum2 = 0.0; for(int i=0; i<20; i++) sum2 += pow(grades[i] - ave, 2); double sd = sqrt(sum2/20);

Exercise Read in a list of positive ints (with the end indicated by a non-positive int) and then say whether or not the list is the same backward as forward (a palindromic list). For example: is, but isn't.