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 9 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()

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”