# CFLib.org – Common Function Library Project

##### Last updated November 15, 2001

Version: 1 | Requires: CF5 | Library: MathLib

Description:
Add two complex numbers that have been stored as a structure using the ComplexNum() UDF. You can also use this function to add a complex and a real number, or even two real numbers. The result is stored in a structure.

Return Values:
Returns a structure.

Example:

``````<cfset a = ComplexNum(3,4)>
<cfset b = ComplexNum(1,-10)>
<cfoutput>
#ComplexNumToString(a)# + #ComplexNumToString(b)# = #ComplexNumToString(c)#<br>
#ComplexNumToString(a)# + 3 = #ComplexNumToString(d)#
</cfoutput>
``````

Parameters:

Name Description Required
First Structure containing a complex number or a real number. Yes
Second Structure containing a complex number or a real number. Yes

Full UDF Source:

``````/**
* Note that this function uses complex numbers stored as structures by the ComplexNum() UDF also available in this library.  The ComplexNum() function is also required for this UDF to function.
*
* @param First      Structure containing a complex number or a real number.
* @param Second      Structure containing a complex number or a real number.
* @return Returns a structure.
* @author Matthew Walker (matthew@electricsheep.co.nz)
* @version 1, November 15, 2001
*/
var ComplexSum = StructNew();
var ComplexFirst = 0;
var ComplexSecond = 0;
var R = 0;
var I = 0;

if ( IsStruct(First) )
ComplexFirst = First;
else
ComplexFirst = ComplexNum(First,0);
if ( IsStruct(Second) )
ComplexSecond = Second;
else
ComplexSecond = ComplexNum(Second,0);

R = ComplexFirst.R + ComplexSecond.R;
I = ComplexFirst.I + ComplexSecond.I;
StructInsert(ComplexSum, "R", R);
StructInsert(ComplexSum, "I", I);
return ComplexSum;
}
``````

### Search CFLib.org

QueryDeleteRows
November 04, 2017

May 11, 2016