Cffunction, cfscript, script, within .cfc files

My background is with VB.net a decade ago, and PowerShell functions as of last week. Functions. Gotta have 'em.

I’m missing something in understanding functions within CF, correct syntax, where they need to be placed, and the correct syntax on how to call them from a(ny) .cfm.

My understanding is that functions should be placed in the/an application.cfc file, although I also understand anything in the application.cfc will be run at launch before the event functions, and I also find many application.cfc files in various sample projects.

I’m seeing that functions can be coded as simply as:

public string function sayHello(mytext) {
    var myFunctionText = myText;
        return myFunctionText;
    }

or as:

<cffunction
	name="OnApplicationStart"
	access="public"
	returntype="boolean"
	output="false"
	hint="Fires when the application is first created.">

	<!--- Return out. --->
	<cfreturn true />
</cffunction>

leaning on the .cfc event functions.

I can save a ‘function’ within the application.cfc without the .cfc failing and returning a blank screen, but I’m not understanding what it looks like to call the function from within a .cfm to pass to it, operate it, and collect the result.

I’ve been trying many combinations but haven’t found one functional.

Usual results are:
No matching function [SAYHELLO] found

Interesting: within index.cfm
13
14 reed = sayhello(“text”)
15 This is #reed# <!— outputting “no”, the default —>
16
17 <cfset reed = sayhello(“text”)/>
18 #reed#

In trying to call sayhello in application.cfc:
public string function sayHello(mytext) {
var myFunctionText = myText;
return myFunctionText;
}

Balks at line 17 "
Lucee 6.0.0.585 Error (expression)
Message No matching function [SAYHELLO] found

Implying that line 14 is valid in at least it’s not bounced…

Good karma in return for any advice…

OK, that understanding is incorrect.

Can I recommend you work through the docs here to start with: https://helpx.adobe.com/coldfusion/developing-applications/building-blocks-of-coldfusion-applications/building-and-using-coldfusion-components.html

That should get you on the right initial track.

At this stage, that’s the best first thing to do rather than ppl offering tutorials via a web forum, I reckon.

2 Likes

I appreciate the link. Much of the “Here’s how to… in CF” bits I’ve been finding were fairly bland (in that they lacked detail). This looks complete and relative.

Ooooh, methods, not functions. Right of the bat…