Here is the AllFunctions cfc file. <cfcomponen...
# cfml-beginners
r
Here is the AllFunctions cfc file. <cfcomponent displayname="Allfunctions" output="false" hint="Handle the application." > <cffunction name="addingFunction" access="public" returnType="numeric"> <cfargument name="firstNumber" type="numeric" required="true"> <cfargument name="secondNumber" type="numeric" required="true"> <cfset var sumOfThem = arguments.firstNumber + arguments.secondNumber> <cfreturn sumOfThem> </cffunction> </cfcomponent> Here is the FuntionTest CFML file. <cfinclude template="AllFunctions.cfc"> <cfoutput> <H1> #AllFunctions.addingFunction(5,2)# </H1> </cfoutput> Yet I get the following error Variable ALLFUNCTIONS is undefined.
m
As far as I know,
<cfinclude />
is used to .cfm files, not .cfc files. Have you looked through the documentation? https://helpx.adobe.com/coldfusion/developing-applications/building-blocks-of-coldfusion-applications/building-and-using-coldfusion-components.html
1
f
@Rick Morayniss if you are familiar with C# then a CFC/Component is analogous to a class, you need to instantiate it first, then you can call functions on it…
👆 1
so if AllFunctions.cfc is in the same folder as your cfm file, then you can do:
Copy code
<cfscript>
allFunc = new AllFunctions();
result = allFunc.addingFunction(1,2);
</cfscript>
if it’s not in the same dir, then you can reference it relative to the web root…
new folder.sub.AllFunctions()
would assume you have the cfc located at:
/folder/sub/AllFunctions.cfc
j
<cfinvoke component=“/pathto/allfunctions(. cfc without the .cfc extension )” method=“addingFunction” result=“resultName”>
Pass arguments using cfinvokeargument. A google search for cfinvoke / cinvokeargument should get you what you want.
You’re obviously using tags, which I love, so - the aforementioned cfscript example can just be ignored. No offense, @foundeo.
f
Haha - none taken!
a
It is worth stating that CFML can be written in cfscript though and not just in cftags - the OP is a C# developer so cfscript may be much easier to follow. That above function in cfscript would look like this:
Copy code
// Allfunctions.cfc
component {
  public numeric function addingFunction(
    required numeric firstNumber,
    required numeric secondNumber
  ) {
    var sumOfThem = firstNumber + secondNumber;
    return sumOfThem;
  }
}
Then can then be called as foundeo posted as:
Copy code
<cfscript>
allFunc = new AllFunctions();
result = allFunc.addingFunction(1,2);
</cfscript>
Note that ColdFusion 2021 and Lucee support static functions which you'll be familiar with in C#
r
Thank you all. foundeo's solution was the perfect fix.