CS 2304 Function Pointers & Lambda Functions

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
ITEC 320 Lecture 24 OO in Ada (2). Review Questions? Inheritance in Ada.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Command-line arguments CS 201 Fundamental Structures of Computer Science.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
CS 240: Data Structures Supplemental: Command Line Input.
C++ Tutorial Rob Jagnow. Overview Pointers Arrays and strings Parameter passing Class basics Constructors & destructors Class Hierarchy Virtual Functions.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
/21 Clang Tutorial CS453 Automated Software Testing.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Lambda Expressions Version 1.0
Fall 2002 CS 325 Class Notes Page 1 Lecture 25 Today –exec() in Unix –CreateProcess in Windows Announcements.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
CS212: Object Oriented Analysis and Design Lecture 28: Functors.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
C:\Temp\Templates 4 5 Use This Main Program 6.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
CSC Programming for Science Lecture 23: More on Function Parameters.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
C++ REVIEW – TEMPLATES. GENERIC PROGRAMMING Programming/developing algorithms with the abstraction of types Algorithms/data is expressed “without type”
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Operator Overloading Introduction
Motivation for Generic Programming in C++
Lambda Expressions Version 1.1
Chapter 6 CS 3370 – C++ Functions.
Lambda Expressions By Val Feldsher.
Command Line Arguments
Command Line Arguments
OOP-4-Templates, ListType
Variables A piece of memory set aside to store data
Chapter Structured Types, Data Abstraction and Classes
Generics, Lambdas, Reflections
CS1061 C Prgramming Lecture 11: Functions
Templates.
Templates in C++.
Templates ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
C Stuff CS 2308.
CS 2308 Exam I Review.
Built-In (a.k.a. Native) Types in C++
Pointers & Functions.
Overview of C++ Overloading
Parasol Lab, Texas A&M University
Pointers and dynamic objects
Pointers & Functions.
Data Structures and ADTs
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Chapter 6 Modular Programming chap6.
各題答對人數.
Chengyu Sun California State University, Los Angeles
Presentation transcript:

CS 2304 Function Pointers & Lambda Functions Gusukuma 2015

Function Pointers What? Why? A pointer to a function Allows the same code to work in multiple instances E.g. sort ascending and sort descending can use the same code with changing the function being used for the comparison Gusukuma 2015

Function Pointer - Syntax ret(*name)(arg, arg) Ret – return type Name – the name of the variable representing the function being pointed to Arg – the type of the arguments the function should take Gusukuma 2015

Function Pointer Example bool anonymousCompare(int x, int y, bool(*comp)(int, int)) { return comp(x, y); } bool lessThan(int x, int y) { return x < y; bool greaterThan(int x, int y) { return x > y; int main(int argc, char* argv[]) { anonymousCompare(3, 2, lessThan); //returns false anonymousCompare(3, 2, greaterThan); //returns true Gusukuma 2015

Lambda functions (stackoverflow.com) What are they? Functions defined directly in source code (as opposed to being defined in a separate function) Functions not bound to an identifier Other names Anonymous function Functional literal Lamda abstraction Gusukuma 2015

Lambda Function Syntax Full Syntax [ capture-list ] ( params ) -> ret { body } Partial Syntax (still valid) [ capture-list ] ( params ) { body } [ capture-list ] { body } Gusukuma 2015

Lambda Function Parts Capture-list Params Body Ret List of variables to pass to the function, like arguments (does not change the function signature), typically local variables Params The parameters that are passed to the function (defines the function signature) Body Where the code for your function goes Ret The return type of the function If not defined, return type is inferred using rules similar to auto Gusukuma 2015

Lambda Function Examples bool myfunction(int i, int j) { return (i<j); } int main() { int myNumba = 123455.304f; auto lambda = [myNumba]() { cout << “lambda: " << myNumba << endl; }; lambda();//prints myNumba int myints[] = { 32,71,12,45,26,80,53,33 }; std::vector<int> myvector(myints, myints + 8); std::sort(myvector.begin(), myvector.end(), [](int i, int j){ return (i<j) }); std::sort(myvector.begin(), myvector.end(), myfunction); } Lambda Expression Lambda Expression Same result as previous line Gusukuma 2015

Lambda Functions vs. Function Pointers One shot Prevents bloating of header files Moves it to the code Better in-line functionality Has the potential to run faster Not bound to a trackable identifier Decreases Modularity Higher coupling (decreased separation of concerns) Passed around multiple times or multiple different functions Can increase clarity of code When functions passed have meaningful names Bound to a trackable identifier Increased separation of concerns Increased modularity Gusukuma 2015