1 11/8/06CS150 Introduction to Computer Science 1 More Functions! 6.10-6.16 page 343 November 8, 2006.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

Chapter 5 Functions.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CS150 Introduction to Computer Science 1
Overview creating your own functions calling your own functions.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
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 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
Computer Science 1620 Function Scope & Global Variables.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS 1400 March 21, 2007 Odds and Ends Review. Reading to the end of a file Example test.txt : … Suppose we need.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 Session-9 CSIT 121 Spring 2006 Lab Demo (first 20 minutes) The correct way to do the previous lab sheet Function calls Calling void functions Calling.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Programming Functions: Passing Parameters by Reference.
CS102 Introduction to Computer Programming Chapter 6 Functions Continued.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
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.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
April 11, 2005 More about Functions. 1.Is the following a function call or a function header? calcTotal(); 2.Is the following a function call or a function.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
A Lecture for the c++ Course
Lab 1 Introduction to C++.
Counting Loops.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Let’s all Repeat Together
Fundamental Programming
Reading from and Writing to Files
CS150 Introduction to Computer Science 1
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Presentation transcript:

1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006

2 11/8/06CS150 Introduction to Computer Science 1 Variable Scope  Scope: where can a variable be used?  Local Scope: variable is only available locally (within a function, loop, etc.) int foo(int x) { int value = x * 2; for( int k = 0; k < value ; k++) { value += (k % 3); } value += k; // ERROR return value; }

3 11/8/06CS150 Introduction to Computer Science 1 Variable Scope  Global Scope: variable is available everywhere in the source code o often a bad idea! int lowervalue = 0; int foo(int x) { int value = x * 2; for( int k = lowervalue; k < value ; k++) { value += (k % 3); } return value; }

4 11/8/06CS150 Introduction to Computer Science 1 Variable Scope  Local variables can hide other variables int lowervalue = 0; int foo(int lowervalue) { int value = lowervalue * 20; for( int k = lowervalue; k < value ; k++) { value += (k % 3); } return value; }

5 11/8/06CS150 Introduction to Computer Science 1 Variable Scope int value = 99; int foo(int lowervalue) { int lowervalue = 20; // ERROR for( int k = lowervalue; k < value ; k++) { value += (k % 3); } return value; }

6 11/8/06CS150 Introduction to Computer Science 1 Practice: What is the result? int number = 0; int foo(int value) { int number = value * 20; for( int value = 0; value < number ; value++) { value += (k % 3); } return value; } int main() { int number = 10; cout << foo(number); return 0; }

7 11/8/06CS150 Introduction to Computer Science 1 Static Local Variables  What happens here? void foo() { int value = 20; cout << “ value: “ << value << endl; value *= 22; } int main() { foo(); }

8 11/8/06CS150 Introduction to Computer Science 1 Static Local Variables  Sometimes we want a function to retain a value between uses o static local variables void foo() { static int value = 20; // This only happens once cout << “ value: “ << value << endl; value *= 2; } What does this do? foo();

9 11/8/06CS150 Introduction to Computer Science 1 Practice: Static Local Variables  Write a function that will count the number of times it has been called and print that count to the screen.  Write a function that will take one integer as a parameter and produce a running sum and running average of the values used as arguments when it is called.

10 11/8/06CS150 Introduction to Computer Science 1 Default Arguments  “Default arguments are passed to the parameters automatically if no argument is provided in the function call” p353 void stars(int numberOfStars = 5) { for( int i=0 ; i < numberOfStars ; i++ ) { cout << “*”; } cout << endl; } What does this do? stars(10); stars();

11 11/8/06CS150 Introduction to Computer Science 1 Default Arguments // specify the default arguments the first time // you define the function void stars(int numberOfStars = 5); int main() { stars(10); stars(); } // do not redefine the default arguments here void stars(int numberOfStars) { for( int i=0 ; i < numberOfStars ; i++ ) { cout << “*”; } cout << endl; }

12 11/8/06CS150 Introduction to Computer Science 1 Practice: Default Arguments  Write a function that will accept either one or two integers as parameters and return the area of a square (if one parameter is specified) or a rectangle (if two parameters are specified)

13 11/8/06CS150 Introduction to Computer Science 1 Overloading Functions  “Two or more functions may have the same name as long as their parameter lists are different.” p365 o return data type is not considered int area(int length); int area(int length, int width); int square(int value); double square(double value); int increment(int value); // ERROR double increment(int value); // ERROR

14 11/8/06CS150 Introduction to Computer Science 1 Practice: Overloaded Functions  Write two overloaded functions that will produce the sum and average of three integers or three doubles.

15 11/8/06CS150 Introduction to Computer Science 1 YourNumber % 3 == 0  Write a small program to read in the following file and print to the screen: o the running sum of the integers read o the running sum of the doubles read o use static local variables and overloaded functions if possible o the first character denotes the data type of the following number Input File: i 4 d 9.1 d 4.0 i 6 i 42 i 9 d 3.0

16 11/8/06CS150 Introduction to Computer Science 1 YourNumber % 3 == 1  Read in the following file and print out a population graph as shown below. The maximum value for a population is Each input file will have exactly five lines of data. Use one star to represent each 1000 people. Input file Output to Screen: 5000 * 4000 * * 3000 * * 2000 * * * 1000 * * * * *

17 11/8/06CS150 Introduction to Computer Science 1 YourNumber % 3 == 2  Read in the following payroll file and display to the screen the wages for each worker. A zero in the file denotes that the worker worked the default of 40 hours or is paid the default of $15 and hour. Input File format: LastName HoursWorked HourlyWage Input File Example: Williams Khoja Ryan 60 0 Output to Screen: Williams: Khoja: Ryan: 900,00

18 11/8/06CS150 Introduction to Computer Science 1 Quiz Solution #include “stdafx.h” #include using namespace std; int main() { ifstream inputFile; int number; inputFile.open( “numbers.dat” ); while(inputFile >> number ) { cout << number << endl; } inputFile.close(); return 0; }