## Tuesday, November 23, 2010

### MathLink Example 1: Sum a List of Integers

This example passes a list of integers, performs the sum and returns an integer.
We must note that the length of the list is not type int but type long.

-------------------------------------------------------------------------
-------------------------------------------------------------------------

```#include "mathlink.h"
extern double addList( double *list, long n);

double addList( double *list, long n)
{

int i;
double sum=0;
for(i=0;i<n;i++) sum = sum + list[i];

return sum;
}

#if __BORLANDC__
#pragma argsused
#endif

int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
{

char  buff[512];
char FAR * buff_start = buff;

char FAR * argv[32];
char FAR * FAR * argv_end = argv + 32;

hinstPrevious = hinstPrevious; /* suppress warning */

if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;

MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
return MLMain( (int)(argv_end - argv), argv);
}

#else

int main(int argc, char* argv[])
{

return MLMain(argc, argv);
}

#endif```

---------------------------------------------------------------------
---------------------------------------------------------------------
```:Begin:

:Arguments:      { L  }
:ArgumentTypes:  { RealList  }
:ReturnType:     Real

:End:

:Evaluate: addList::usage = "addList[x] gives the sum of the elements of the list x"```

---------------------------------------------------------------------
Makefile
---------------------------------------------------------------------
```MPREP = /usr/bin/mprep
CXX = /usr/bin/c++

all : \$(BINARIES)