# Message Boards

Posted 7 years ago
2954 Views
|
0 Replies
|
5 Total Likes
|
 In a previous post I explained how to pass in and return strings using LibraryLink. In this post we will be doing somethingsimilar, but with passing in an integer value, performing a basic arithmetical operation on it and returning the result. In particularwe will be squaring the integer being passed in.Here is the C code for 'Squared', which we will put in a file called Squared.c:  #include "WolframLibrary.h"  DLLEXPORT int Squared(WolframLibraryData libData, mint Argc, MArgument *Args, MArgument Res) {         mint x;         mint result;         x = MArgument_getInteger(Args);         result = x*x;         MArgument_setInteger(Res,result);        return LIBRARY_NO_ERROR;}Note how this code is very similar to the previous Greetings example, except that we are now passing in integers (using MArgument_getInteger)and returning integers (MArgument_setInteger). The squaring operation is performed by x*x and should work for small enough values of x. Thisis then assigned to 'result' which is passed back to the Wolfram Language function.The Wolfram Language build and function loading code is also similar to the Greetings example, but now we need to specify Integerinstead of "UTF8String" (note that Integer is a Wolfram Language symbol here and not a string).Needs["CCompilerDriver`"];lib = CreateLibrary[{"Squared.c"},"Squared"];Squared = LibraryFunctionLoad[lib,"Squared",{Integer},Integer];Now we can try running this code in the Wolfram Language:  Mathematica 10.0 for Microsoft Windows (64-bit) Copyright 1988-2013 Wolfram Research, Inc.  In:= Get["Squared.m"]  In:= Squared  Out= 16In:= SquaredOut= 1764Note that if you call the function Squared with a value that is too large, an error will be issued:In:= SquaredLibraryFunction::cfsa: Argument 58934754879783459483793 at position 1 should be a machine-size integer.Out= LibraryFunction[<>, Squared, {Integer}, Integer]In the next post we will take a look at how to create a function which returns a boolean answer (True/False). Answer