lec11 - CS240 Programming in C Lecture 11 Function Pointers...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
CS240: Programming in C Lecture 11: Function Pointers Sunday, March 20, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Abstractions in Programming How are abstractions manifested in languages? As structures that encapsulate code and data providing information hiding E.g., a Java class As program structures that refactor common usage patterns E.g., a sorting routine that can sort lists of different types The two notions are obviously related public C m1(C’ o) { . .. o.m(. ..) . ..} Can be applied to any object of instantiated from class C’ or its subclasses The context in which M is applied must be one that expects objects of type C or any of its superclasses Sunday, March 20, 2011
Background image of page 2
Abstractions in C C doesn’t provide data abstractions like Java classes There is no easy or obvious way to package related data and code within a single structure Hard to enforce information hiding But, it does a provide a useful refactoring mechanism Functions are the most obvious example They abstract a computation over input arguments What kinds of arguments can these be? 3 Sunday, March 20, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Types and Computation Functions can be abstracted over basic types (e.g., int, float, double,. ..) structured types (e.g., structs, unions, . ..) These types can be thought of as primitive data abstractions They represent a set of values along with operations on them What about functions themselves? They’re obviously a form of abstraction Rather than representing a set of values, they represent a set of computations abstracted over arguments of a fixed type There is exactly one operation allowed on function types: application Sunday, March 20, 2011
Background image of page 4
Types Following this line of thought: A type (or a data abstraction) is a set of values equipped with a set of operations on those values A function is a computation abstracted over the types defined by its inputs Hence, a function is an abstraction : it represents the set of values produced by the computation it defines when instantiated with specific arguments. Thus, its type is characterized by its argument types and the result of its computation Hence, functions should be allowed to be abstracted over function types , just as they are allowed to be abstracted over primitive and structure types 5 Sunday, March 20, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
.. C permits functions (more accurately, function pointers) to be treated like any other data object A function pointer can be supplied as an argument Returned as a result Stored in any array Compared, etc. Main caveat:
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 29

lec11 - CS240 Programming in C Lecture 11 Function Pointers...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online