Passing Functions as Parameters

Slides:



Advertisements
Similar presentations
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Structures Spring 2013Programming and Data Structure1.
Gerardo Schneider Department of Informatics University of Oslo December 2008.
Advanced Programming Parameter Passing Giuseppe Attardi Università di Pisa.
Procedures and Control Flow CS351 – Programming Paradigms.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
Parameter Passing. Expressions with lvalues and rvalues An expression has an lvalue/rvalue if it can be placed on the left/right side of an assignment.
Overview Parameter passing modes.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 9 Functions It is better to have 100 functions.
Chapter 8 :: Subroutines and Control Abstraction
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
Runtime Environments Compiler Construction Chapter 7.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Basic Semantics Associating meaning with language entities.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Programming Languages
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.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Week 12 Methods for passing actual parameters to formal parameters.
Names, Scope, and Bindings Programming Languages and Paradigms.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Programming Language History and Evolution
Chapter 1. Introduction.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Functions.
Introduction to Programming Using C
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
C Language By Sra Sontisirikit
Principles of programming languages 4: Parameter passing, Scope rules
Learning Objectives Pointers Pointer in function call
Programming Language History and Evolution
Using local variable without initialization is an error.
Chapter 10: Implementing Subprograms Sangho Ha
Names, Scopes, and Bindings: Scopes
PZ06C - Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3 PZ06C.
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Subprograms and Programmer Defined Data Type
Topics discussed in this section:
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Subroutines – parameter passing
Pointers (continued) Chapter 11
Overview of Programming Paradigms
Pointers Pointers point to memory locations
Programming Languages and Paradigms
A simple function.
Parameter transmission
PZ09B - Parameter transmission
Topics discussed in this section:
Parameter transmission
Parameter transmission
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

Passing Functions as Parameters

Functions as entities Third class entities: The only thing you can do with a function is call it. Early Fortran, Ada-83. Second class entities: You can also pass it as an argument to another function. Pascal. First class entities (defn 1): You can also return it as the value of a function call or assign it as a value of a variable. First class entities (defn 2): You can also generate new functions at run time. Functional languages (Lisp, ML), many scripting language (C#, Perl, Python).

In C void applyToArray(fp,a,b,n) int (*fp)(), a[], b[],n) { for (int i=0; i<n; i++) b[i] = (*fp)(a[i]); } int square(int N) { return(N*N); } void main() { int a[3] = {1,2,3}, b[3]; applyToArray(square,a,b,3); for(int i=0; i<3; i++) printf(“%i “, i); }

Implementation main passes starting address of “square” as argument to applyToArray. applyToArray finds value of fp to be address of “square”, jumps there. Otherwise, same as any other calling sequence. Note: The type returned by fp is declared, but neither the type nor number of its arguments are declared.

Other operations on function pointers in C You can assign to a function pointer variable, but that’s pretty much all. int (*powerFunction)() (int n) { int (*fp)(), i; fp = square; i = (*fp)(3); …

C++ C++ is similar, except that Declare types of arguments Can return function as value of function. typedef int (*MapsIntToInt) (int); MapsIntToInt fp; fp = square;