Functions as parameters general issue c++ java. What? Typically programmers use data structures as the means to give specific definition to a general.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 10 Pointers and Dynamic Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Pointers Pointer variables.
C Language.
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.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
CPSC 388 – Compiler Design and Construction Parameter Passing.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
© Vinny Cahill 1 Classes in Java. © Vinny Cahill 2 Writing a Java class Recall the program to calculate the area and perimeter of a rectangle of given.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Object Oriented Programming Lecture 7: Algorithm animation using strategy and factory patterns, The Adapter design pattern
Introduction to Object Oriented Programming CIS
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
Bubble Sort Notes David Beard CS181. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
OOP Etgar 2008 – Recitation 51 Object Oriented Programming Etgar 2008 Recitation 5.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
Assembly Language Part 5. Reference parameter/global variable model C++ reference parameters are references to the actual arguments (as opposed to copies.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
CS 1704 Introduction to Data Structures and Software Engineering.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Hank Childs, University of Oregon May 13th, 2015 CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __.
Exam Format  90 Total Points  60 Points Writing Programs  25 Points Tracing Code/Algorithms and determining results  5 Points Short Answer  Similar.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Function Pointers Gabriel Hugh Elkaim Spring 2013.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Overview of C++ Polymorphism
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Generic Programming in C
Recursion DRILL: Please take out your notes on Recursion
CS 3305 System Calls Lecture 7.
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Pointers & Functions.
Inheritance: Polymorphism and Virtual Functions
Defining Classes and Methods
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Inheritance: Polymorphism and Virtual Functions
Function “Inputs and Outputs”
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Overview of C++ Polymorphism
Pointers & Functions.
Inheritance: Polymorphism and Virtual Functions
Lecture 6: Polymorphism
2D Shapes Rectangle Circle Triangle Rectangle. What shape is the door? Rectangle.
Introduction to Methods and Interfaces
Presentation transcript:

Functions as parameters general issue c++ java

What? Typically programmers use data structures as the means to give specific definition to a general algorithm. Parameters allow us to abstract from examples how the various uses can be viewed as being similar. Area of a square = s 2 Area of a rectangle = l x w Rectangle is a generalization of a square. To use the general function for a square – area (s,s) for a square.

What if area is more complicated? What if the generalization does not adapt that easily? –Circles? –I.e. what if you have to use integration to calculate the area of the shape? Generalize the function to allow you to define the calculation. dimension area dimension area function def

Another example Sort routine to sort arbitrary objects Sort algorithm is the same for all objects –Bubble sort, quick sort, etc Comparison is dependent on the object Array of objects Comparison method

Conclusion Using parameters allows for generalization and broader applicability of a function Generalizing the functionality itself goes even further in widening the use if an operation is parameterized. This is very much like using base classes in OOP to access more specific functionality in derived classes. –Standard example – area of figures

C++ example: function pointers… first step #include //definition of 1 void func1(int a) { int r = a+2; cout << "Function 1 call result " << r << endl; } //definition of 2 void func2(int a) { int r = a*2; cout << "Function 2 call result " << r << endl; } void main(){ void (*fp)(int); // pointer to a void function // with a single integer param fp = func1; (*fp)(5); // call func1 fp = func2; (*fp)(5); // call func2 } outputs 7 outputs 10

C++ example: functions as parameters #include void func1(int a) { int r = a+2; cout << "Function 1 call result " << r << endl; } void func2(int a) { int r = a*2; cout << "Function 2 call result " << r << endl; } void g( void (*f)(int), int x) { f(x); } void main(){ g( func1, 5); g( func2, 5); } outputs 7 outputs 10

Other more common uses Windows programs use them for event handlers. –Define method/handler –Pass method to Operating System –Let OS call your method instead of you calling it Threads –You define the run() method. –The OS will run this function as a thread using your run() function to start it Called CALLBACKS. Like giving someone a phone number to use to call you back.

Threads c++: another example void count(int i) {int j; for (j=1; j<=i; j++) cout << j<<endl; } void main() { _beginthread(( (void(*) void()) count, 0, (void*) 5); count(4); }

Variations on the theme Window GUI handlers do actually get a method to use in calling C++ threads really do pass function pointers to the OS to use in invoking threads –Nasty! Java does NOT actually pass a method name to the OS, rather uses a standard name. –Get objects that adhere to a “interface”

So how does java do it? Everything already is a reference Define an interface specification Create a class abiding by that interface Pass a reference to an object conforming to that interface Execute that reference. Does NOT allow passing function references