Science Fair Projects Ideas - Function object

All Science Fair Projects

      

Science Fair Project Encyclopedia for Schools!

  Search    Browse    Forum  Coach    Links    Editor    Help    Tell-a-Friend    Encyclopedia    Dictionary     

Science Fair Project Encyclopedia

For information on any area of science that interests you,
enter a keyword (eg. scientific method, molecule, cloud, carbohydrate etc.).
Or else, you can start by choosing any of the categories below.

Function pointer

A function pointer is a type of pointer in the C and C++ programming languages. A function pointer, when dereferenced, calls a function with arguments placed after the dereferenced pointer just like a normal function. Function pointers are of the type the function returns.

An Example in C

Suppose we have a linked list containing integer values. We want to perform two operations on this list: 1) sum up all values, and 2) calculate the product of all values:


#include <stdio.h>

int sum = 0;             /* store sum */
int product = 1;         /* store product */

void fsum(int value)
{ sum += value; }

void fproduct(int value)
{ product *= value; }

void map_function_on_list(list *L, void (*functionptr)(int))
{
  listnode *node;
  node = L->first;
  while (node != NULL)
  {
    functionptr(node->value); /* call function pointer */
    node = node->next;
  }
}

int main()
{
  list *L; 
  ... fill list with values ...
  
  map_function_on_list(L, fsum);     /* calculate the sum */
  map_function_on_list(L, fproduct); /* calculate product */

  printf("Sum: %d\nProduct %d\n", sum, product); /* display results */
}

Clearly, function pointers provide a powerful mechanism for programming. Imagine rewriting the above code without function pointers. The fsum() and fproduct() function would both require loops iterating through the linked list. What if we also want to have functions that subtract, divide, find the max/min value, etc? Function pointers are the clear choice of implementing such functions.

Function objects, or functors, are similar to function pointers, but in some ways they are more flexible. For example, you can use a functor to emulate a closure.

Function Values

Similar to function pointers, function values provide a mechanism for pass functions as arguments. In certain languages, such as ML, functions can be stored (or mapped to) values. This allows passing expressions as arguments. To demonstrate this, the following example determines if the elements in a list are even or odd:


fun map(F, nil) = nil
  | map(F, x::xs) = F(x)::map(F, xs);

map(fn x => x mod 2, [1,2,3,4,5,6,7,8,9]);

The function map() takes a value and a list. In this case, the value we pass is a function, specificially x mod 2. This function is recursively applied to each element in the list.

Another name for a function value is a first class function.

External Link

03-10-2013 05:06:04
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
Science kits, science lessons, science toys, maths toys, hobby kits, science games and books - these are some of many products that can help give your kid an edge in their science fair projects, and develop a tremendous interest in the study of science. When shopping for a science kit or other supplies, make sure that you carefully review the features and quality of the products. Compare prices by going to several online stores. Read product reviews online or refer to magazines.

Start by looking for your science kit review or science toy review. Compare prices but remember, Price $ is not everything. Quality does matter.
Science Fair Coach
What do science fair judges look out for?
ScienceHound
Science Fair Projects for students of all ages
All Science Fair Projects.com Site
All Science Fair Projects Homepage
Search | Browse | Links | From-our-Editor | Books | Help | Contact | Privacy | Disclaimer | Copyright Notice