1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.

Slides:



Advertisements
Similar presentations
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Advertisements

1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
CS150 Introduction to Computer Science 1
Function Overloading Can enables several functions Of same name Of different sets of parameters (at least as far as their types are concerned) Used to.
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.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
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.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Multiple-Subscripted Array
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
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.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
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.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Variable Scope Storage Class Recursion
CS102 Introduction to Computer Programming Chapter 6 Functions Continued.
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.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 10 Inheritance and Polymorphism
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
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.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
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.
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.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Arrays Chapter 12. One-Dimensional Arrays If you wanted to read in 1000 ints and print them in reverse order, it would take a program that’s over 3000.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
 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.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Procedural and Object-Oriented Programming
Suppose we want to print out the word MISSISSIPPI in big letters.
New Structure Recall “average.cpp” program
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CS150 Introduction to Computer Science 1
Counting Loops.
CS150 Introduction to Computer Science 1
Functions 2: Stupid Function Tricks
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS1201: Programming Language 2
CS150 Introduction to Computer Science 1
Presentation transcript:

1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8

2 Review  Functions o Prototype o Call o Definition  Passing arguments o By value o By reference

3 Arrays as Function Arguments (8.8)  You can pass an array as an argument to a function void printIntArray(int arr[], int size); int main() { int values[] = {5, 6, 0, 1, 2, 3, 4}; const int SIZE = 7; printIntArray(values, SIZE); return 0; } Array Name

4 Arrays as Function Arguments void printIntArray(int arr[], int size) { for(int i = 0; i < size; i++) { cout << arr[i] << endl; }

5 Passing Single Array Elements  Passing single array elements is like passing in a single variable void showValue(int num); int main() { int values[] = {5, 6, 0, 1, 2, 3, 4}; const int SIZE = 7; for(int i = 0; i < SIZE; i++) { showValue(values[i]); } return 0; }

6 Passing Arrays into Functions  Arrays are always passed by reference  What does this mean?

7 Q.1. Practice  Write a function that will accept an integer array and a size for that array, and return the sum of all the elements in the array o int sumArray(int array[], int size);

8 Q.2. Practice  Write a function that will accept an integer array and a size for that array and return the highest value in the array o int getHighest(int array[], int size);

9 Variable Scope (6.10)  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; }

10 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; }

11 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; }

12 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; }

13 Q.3. Practice: What is the result? int number = 0; int foo(int value) { int number = value * 10; for(int value = 0; value < number; value++) { value += number; } return value; } int main() { int number = 2; cout << foo(number); return 0; }

14 Q.4. Static Local Variables (6.11)  What happens here? void foo() { int value = 20; cout << “ value: “ << value << endl; value *= 22; } int main() { foo(); }

15 Q.6. Static Local Variables  Sometimes we want a function to retain a value between uses o static local variables void foo() { static int value = 20; cout << “ value: “ << value << endl; value *= 2; } int main() { foo(); }

16 Q.7. Practice: Static Local Variables  Write a function that will count the number of times it has been called and print that 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.

17 Default Arguments (6.12)  “Default arguments are passed to the parameters automatically if no argument is provided in the function call” p343 void stars(int numberOfStars = 5) { for(int i = 0; i < numberOfStars; i++) { cout << “*”; } cout << endl; } int main() { stars(10); stars(); }

18 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; }

19 Q.8. 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)

20 Overloading Functions (6.14)  “Two or more functions may have the same name as long as their parameter lists are different.” p354 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

21 Q.9. Practice: Overloaded Functions  Write two overloaded functions that will produce the sum and average of three integers or three doubles.