# Example Dictionary mapc 002

#### Description

The following example will define a user function, total, that will take an argument (which is a dictionary field) and add the argument to a pvar container (persistent variable). The input to the total function is a dictionary containing monthly sales figures. The mapc function then applies each Dictionary element as argument to the function total.

#### Details

Defines the Dictionary Monthly Sales.

 (define MonthlySales #{dic|Jan: 1000 Feb: 2300 Mar: 3200 Apr: 4500}) Returns: #

Defines the function total.

 (defun total(x) pvars: (n) (setq n (+ n x))) Returns: #

The result is saved in the persistent variable, n.

 (display total.n) Returns: 11000

The following code sample using a loop, produces the same result as mapc:

 (define MonthlySales #{dic| Jan: 1000 Feb: 2300 Mar: 3200 Apr: 4500}) Returns: #
 (defun total(x) pvars: (n) (setq n (+ n x))) Returns: #
 (loop for n from 0 until (length MonthlySales) do (total MonthlySales[i]) ) Returns: 4

The result is saved in the the persistent variable, n

 (display total.n) Returns: 11000

Notes and Hints

Note, the return value from the loop is the number of iterations performed. In this case, it is the number 4, whereas the return value from the mapc function is the value of the function as it is applied to the last member of the dictionary, 11000.

The mapc function does not produce a duplicate dictionary as in the map function.

#### Related Examples

Here are examples of the mapc function at work.

 Dictionary mapc 002 Directory mapc 003 FltVector mapc 005 IntVector mapc 006 List mapc 009 Matrix mapc 005 NumMatrix mapc 009 NumVector mapc 007 Pair mapc 008 ShortVector mapc 007 Structure mapc 001 Vector mapc 004

Here is the link to the current function used in this example.

mapc

Here are a number of links to other related functions.

 map(#void) makeDictionary(#void)

#### Argument Types

Here are the links to the data types of the arguments used in this example.

 Dictionary

Here are a number of links to examples having similar argument types.