SlideServe
Menu
Préférences
© SlideServe · v2026

Passing Functions as Parameters

camdyn avatar
6 diapositives · 175 Views · Oct 15, 2014

This article explores the concept of function pointers and their usage across various programming languages, including how to pass functions as parameters, return them as values, and manipulate them dynamically during runtime. Examples and comparisons are provided for clarity.

Passing Functions as Parameters - slide 1
01 / 6Passing Functions as Parameters
1 / 17

Télécharger la présentation

Passing Functions as Parameters
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Passing Functions as Parameters

  2. 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).

  3. 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); }

  4. 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.

  5. 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); …

  6. 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;