CS150 Introduction to Computer Science 1

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
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.
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.
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.
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.
CS102 Introduction to Computer Programming Chapter 6 Functions Continued.
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.
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.
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.
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.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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.
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
Procedural and Object-Oriented Programming
Test 2 Review Outline.
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?
Two Dimensional Array Mr. Jacobs.
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.
6 Chapter Functions.
CS150 Introduction to Computer Science 1
Counting Loops.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Functions 2: Stupid Function Tricks
CS150 Introduction to Computer Science 1
Namespaces How Shall I Name Thee?.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
The Function Prototype
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
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
Fundamental Programming
CS250 Introduction to Computer Science II
CS150 Introduction to Computer Science 1
The Stack.
CS1201: Programming Language 2
Class: Special Topics Overloading (methods) Copy Constructors
CS150 Introduction to Computer Science 1
Presentation transcript:

CS150 Introduction to Computer Science 1 Functions Chapter 6, 8 11/19/2008 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Review Functions Prototype Call Definition Passing arguments By value By reference 11/19/2008 CS150 Introduction to Computer Science 1

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 11/19/2008 CS150 Introduction to Computer Science 1

Arrays as Function Arguments void printIntArray(int arr[], int size) { for(int i = 0; i < size; i++) cout << arr[i] << endl; } 11/19/2008 CS150 Introduction to Computer Science 1

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; 11/19/2008 CS150 Introduction to Computer Science 1

Passing Arrays into Functions Arrays are always passed by reference What does this mean? 11/19/2008 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 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 int sumArray(int array[], int size); 11/19/2008 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 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 int getHighest(int array[], int size); 11/19/2008 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 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; 11/19/2008 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Variable Scope Global Scope: variable is available everywhere in the source code 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/19/2008 CS150 Introduction to Computer Science 1

CS150 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; 11/19/2008 CS150 Introduction to Computer Science 1

CS150 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; 11/19/2008 CS150 Introduction to Computer Science 1

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; 11/19/2008 CS150 Introduction to Computer Science 1

Q.4. Static Local Variables (6.11) What happens here? void foo() { int value = 20; cout << “ value: “ << value << endl; value *= 22; } int main() foo(); 11/19/2008 CS150 Introduction to Computer Science 1

Q.6. Static Local Variables Sometimes we want a function to retain a value between uses static local variables void foo() { static int value = 20; cout << “ value: “ << value << endl; value *= 2; } int main() foo(); 11/19/2008 CS150 Introduction to Computer Science 1

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. 11/19/2008 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 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(); 11/19/2008 CS150 Introduction to Computer Science 1

CS150 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; 11/19/2008 CS150 Introduction to Computer Science 1

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) 11/19/2008 CS150 Introduction to Computer Science 1

Overloading Functions (6.14) “Two or more functions may have the same name as long as their parameter lists are different.” p354 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 11/19/2008 CS150 Introduction to Computer Science 1

Q.9. Practice: Overloaded Functions Write two overloaded functions that will produce the sum and average of three integers or three doubles. 11/19/2008 CS150 Introduction to Computer Science 1