Is it possible to call a function from within a for loop?

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
HappyShat
Full Member
Posts: 64
Joined: Thu Jul 25, 2019 9:43 pm

Is it possible to call a function from within a for loop?

Post by HappyShat » Sun Nov 09, 2025 2:03 am

I'm having an issue with QCAD's JavaScript. I've created a function that works just fine outside of a for loop - but the moment I place the function inside a for loop I get an error.

this is the error message I get:

Code: Select all

TypeError: Result of expression 'tw' [3] is not a function.
If there's something fundamental about the scope of the function prohibiting use from within a for loop - then I could skip using the function if need be. But, I feel I must have missed something (i.e. this is possible - and I'm just implementing my code incorrectly).

I'm using QCAD Version: 3.24.3.0 (3.24.3) on a Windows 11 Pro system.

Below is my code - the function is called tw()

Thanks for any help or insight.

=HS

Code: Select all

function tw(angle, we, ws, steps) {  // provide trace width for given angle; Note 
  
  tw = ws + ((we-ws)/steps)*angle;
  
  return tw;
}

var rs = 15; // start radius (inner most line)
var ws=20; // trace width begin
var we=3; // trace width end (of 2*pi*N)
var g=1; // gap between traces
var N=2; // Number of turns 

var steps=100; // steps per 2*pi*N

var aDelta = 2 * Math.PI * N / steps;
var wDelta = (we-ws)/steps;


re= 2*tw(steps, we, ws, steps) + 2*g; // function works here

var r1Delta = (re-rs)/steps;  //  

r1=rs; 

a=0;
for (var n=0; a<(2*Math.PI * N); n+=1) {
		
		a = (2 * Math.PI * N / steps)*n;  
		xd = 2*tw(steps, we, ws, steps) + 2*g;  // function doesn't work inside for loop.  
		// inner most line
	}
Attachments
function_inside_for_loop.js
(758 Bytes) Downloaded 64 times

HappyShat
Full Member
Posts: 64
Joined: Thu Jul 25, 2019 9:43 pm

Re: Is it possible to call a function from within a for loop?

Post by HappyShat » Sun Nov 09, 2025 2:29 am

I believe I found the problem with my code.

I didn't use var to declare the variable tw inside the function tw()

CVH
Premier Member
Posts: 4993
Joined: Wed Sep 27, 2017 4:17 pm

Re: Is it possible to call a function from within a for loop?

Post by CVH » Tue Nov 11, 2025 9:39 pm

Hi,

It is never a good idea to mix the same name for a variable and for a function.

tw inside function tw but after an = would call the function without arguments.

In QCAD it is common that functions start with a capital and variables don't.
This is not mandatory in ECMAScript but it helps you to distinguish between them.

An additional idea is the use of self-explanatory function names.

Instead of defining the variable tw to return you could simply return the resulting value:

Code: Select all

// provide trace width for given angle
function TraceWidth(angle, we, ws, steps) {
    return ws + ((we - ws) / steps) * angle;
}
function doesn't work inside for loop.
What happens is that on the first run it overwrites the function definition.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”