324-lecture18 - Principles of Programming Languages...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Principles of Programming Languages Principles of Programming Languages Lecture 18 Lecture 18 University of Toronto Wael Aboulsaadat Wael Aboulsaadat wael@cs.toronto.edu wael@cs.toronto.edu http://portal.utoronto.ca/ 1 Function types A type that allow an object to be invoked or called as if it were an ordinary function E.g. Sorting an array containing a user defined type // Declaration of C sorting function void sort ( int itemlist, int numitems, int (*cmpfunc)(item*, item*) ); ... Function Type Function Type University of Toronto // Callback function int compare_function( item * A, item* B) { // // } . sort( itemlist, numitems, compare_function); Function types A type that allow an object to be invoked or called as if it were an ordinary function E.g. using different sorting algorithm // Declaration of C sorting function void bubblesort ( int itemlist, int numitems, int (*cmpfunc)(int*, int*) ) { . } Function Type Function Type University of Toronto void quicksort ( int itemlist, int numitems, int (*cmpfunc)(int*, int*) ) { . } void doSomething(.) { ... func1(itemlist , numitems , bubblesort); func2(itemlist , numitems , quicksort); } void func1(.,sortfunc ) { sortfunc(itemlist,numitems) } void func2(.,sortfunc ) { sortfunc(itemlist,numitems) } Primitive types Integer, Float, Boolean, Char Structured Types Strings, Ordinal, Arrays, Associative Arrays, Records, Union, Lists bject Type Data types Summary Data types Summary University of Toronto Object Type Class Type Function Type 4 Note: Dont confuse data types with API data structures (BST, Graphs,) Type Conversions Type Conversions Often want to write expressions that are mixed mode (contain operands of more than one type) real + integer This implies a need to convert one type to another so the expression can be evaluated coercion is implicit conversion , done automatically by the compiler University of Toronto implies semantics that define rules for determining type to convert to from operands problem is loss of error detection casts are explicit conversions specified by the programmer can lead to very clumsy expressions if doing a lot of mixed mode expressions int ( Index ) ; // Ada and Python (int) Index; // C and Java 5 Type Conversions Type Conversions - contd contd Whether implicit or explicit, conversions can be widening convert to a type with a greater representation range although perhaps with a loss of precision integer b real narrowing nvert to a type with a more restricted range University of Toronto convert to a type with a more restricted range double precision b real promoting convert to a type with additional semantic information integer b character e.g. NewChar = chr(IntValue) demoting strip away semantic information character b integer 6 Type Conversion Type Conversion examples examples...
View Full Document

Page1 / 28

324-lecture18 - Principles of Programming Languages...

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

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