CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Function Pointers Gabriel Hugh Elkaim Spring 2013.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

C Language.
Spring Semester 2013 Lecture 5
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
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.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Functions Functions and Parameters. History A function call needs to save the registers in use The called function will use the registers The registers.
CMPE-013/L Expressions and Control
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Presentation Title. Presentation Title (can go two or three lines) This Font is Georgia/bold Template Instructions To edit the presentation title above:
CMPE 293Gabriel Hugh Elkaim 1. CMPE 293Gabriel Hugh Elkaim 2.
CMPE 293Gabriel Hugh Elkaim 1. CMPE 293Gabriel Hugh Elkaim 2.
Functions as parameters general issue c++ java. What? Typically programmers use data structures as the means to give specific definition to a general.
CMPE 293Gabriel Hugh Elkaim 1 CMPE 293: Intro to Feedback Control Design Gabriel Hugh Elkaim.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
Computer Programming 1 More on functions. Computer Programming 2 Objectives Function overloading Scope rules and namespace Inline Templates Pass by value.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
Pointers and Arrays C and Data Structures Baojian Hua
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
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);
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Functions Gabriel Hugh Elkaim Spring 2012.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Engineering a Cat Door Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Constants and Literals Gabriel Hugh Elkaim Spring 2012.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Structures Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Introduction to “C” Programming Gabriel Hugh Elkaim Spring 2012.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Pointers Gabriel Hugh Elkaim Spring 2012.
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Unions and Bitfields Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Computer Systems and “C” Programming Gabriel Hugh Elkaim Spring 2013.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Enumerations Gabriel Hugh Elkaim Spring 2013.
1 Data Structure & Algorithm Pointer & Class. 2 Pointer can be used to store the address of other variables with types of int, char, float, and double.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Exercises in MPLAB-X Gabriel Hugh Elkaim Spring 2013.
W1-1 University of Washington Computer Programming I Recursion © 2000 UW CSE.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Fundamental Programming Fundamental Programming Introduction to Functions.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Functions and an Introduction to Recursion.  Write a program for learn C++ subfunction.  Exercise: ◦ Please implement the following functions:  double.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Instructions for test_function
Chapter 5: Enhancing Classes
User-Written Functions
Hassan Khosravi / Geoffrey Tien
A Lecture for the c++ Course
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Templates.
Passing Functions as Parameters
Name: Rubaisha Rajpoot
Find the Taylor series for f (x) centered at a = 8. {image} .
C-to-LC3 Compiler Over the course of the next two weeks, you will build a program that will compile C code to LC-3 assembly language Don't panic! You.
Dynamic Memory A whole heap of fun….
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Homework Any Questions?.
Module 2-3: Passing pointers
The Stack.
CS1201: Programming Language 2
Presentation transcript:

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Function Pointers Gabriel Hugh Elkaim Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Function Pointers Pointers may also be used to point to functions Provides a more flexible way to call a function, by providing a choice of which function to call Makes it possible to pass functions to other functions Not extremely common, but very useful in the right situations

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 A function pointer is declared much like a function prototype: Here, we have declared a function pointer with the name fp –The function it points to must take one int parameter –The function it points to must return an int Function Pointers Declaration int (*fp)(int x);

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 A function pointer is initialized by setting the pointer name equal to the function name Function Pointers Initialization If we declare the following: We can initialize the function pointer like this: fp = foo; //fp now points to foo int (*fp)(int x); //Function pointer int foo(int x); //Function prototype

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Function Pointers Calling a Function via a Function Pointer The function pointed to by fp from the previous slide may be called like this: This is the same as calling the function directly: y = foo(x); y = fp(x);

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Function Pointers Passing a Function to a Function Example 1: Understanding the Mechanism int x; int foo(int a, int b); //Function prototype int bar(int a, int b); //Function prototype //Function definition with function pointer parameter int foobar(int a, int b, int (*fp)(int, int)) { return fp(a, b); //Call function passed by pointer return fp(a, b); //Call function passed by pointer} void main(void) { x = foobar(5, 12, &foo); //Pass address of foo x = foobar(5, 12, &foo); //Pass address of foo}

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Function Pointers Passing a Function to a Function Example 2: Evaluate a Definite Integral (approximation) float integral(float a, float b, float (*f)(float)) { float sum = 0.0; float sum = 0.0; float x; float x; int n; int n; //Evaluate integral{a,b} f(x) dx //Evaluate integral{a,b} f(x) dx for (n = 0; n <= 100; n++) for (n = 0; n <= 100; n++) { x = ((n / 100.0) * (b – a)) + a; x = ((n / 100.0) * (b – a)) + a; sum += (f(x) * (b – a)) / 101.0; sum += (f(x) * (b – a)) / 101.0; } return sum; return sum;} a b  y = f(x) dx Adapted from example at: bounds of integralfunction to be evaluated

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab Exercise 13 Function Pointers

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab 13 Function Pointers Open the lab Project: 1 1 Open MPLAB ® X and select Open Project Icon (Ctrl + Shift + O) Open the Project listed above. If you already have a project open in MPLAB X, close it by “right clicking” on the open project and selecting “Close” /Examples/Lab13.zip -> Load “Lab13.X” On the class website

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab 13 Function Pointers Compile and run the code: 2 2 Click on the Debug Project button. Debug Project Continue If no errors are reported, click on Continue button to start the program. Pause Click on the Pause button.

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Lab 13 Function Pointers Results Three separate functions are integrated over the interval 0 to 1: y 1 = ∫ x dx= ½ x 2 + C [0,1] = y 2 = ∫ x 2 dx= ⅓ x 3 + C [0,1]= y 3 = ∫ x 3 dx= ¼ x 4 + C [0,1]=

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 13 Function Pointers /*============================================================================ FUNCTION: xsquared() DESCRIPTION: Implements function y = x^2 PARAMETERS: float x RETURNS: float (x * x) REQUIREMENTS: none ============================================================================*/ float xsquared(float x) { return (x * x); } /* Evaluate y2 = Int x^2 dx over the interval 0 to */ y2 = integral(0, 1, xsquared); Function to Evaluate: xsquared()

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 13 Function Pointers /*============================================================================ FUNCTION: integral() DESCRIPTION: Evaluates the integral of the function passed to it over the interval a to b. PARAMETERS: interval end points a & b and function to integrate RETURNS: integral of function f over interval a to b REQUIREMENTS: none SOURCE: Adapted from example at: ============================================================================*/ float integral(float a, float b, float (*f)(float)) { float sum = 0.0; float x; int n; //Evaluate integral{a,b} f(x) dx for (n = 0; n <= 100; n++) { x = ((n / 100.0) * (b-a)) + a; sum += (f(x) * (b-a)) / 101.0; } return sum; }

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Exercise 13 Conclusions Function pointers, while not frequently used, can provide a very convenient mechanism for passing a function to another function Many other possible applications exist –Jump tables –Accommodating multiple calling conventions –Callback functions (used in Windows™) –Call different versions of a function under different circumstances

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 Questions?

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013