1 страниц (3 вхождений)
"Variable already defined" error when using a function - Function works 1st time but not after that - Сообщения
#1 Опубликовано: 04.06.2025 02:08:37

Trying to make a finite element beam calculator. I've already done this in Python just trying to implement my code in SMath as I was hoping to make a more readable version, but it has been a real struggle. My current struggle is trying to understand why a function I created to override the load matrix with 0s only works as intended once, and then throws a "Variable already defined by value of another type" after that. Shouldn't everything in the function be a local variable and be defined fresh every time based off of the arguments? I don't see how trying to call it again with different arguments should produce an error.
#2 Опубликовано: 04.06.2025 08:46:03
Hello Dave,
Hard to say without a worksheet, because there are variables from outside the function involved.
Nope.
By default "Global" variables (from the canvas, or from a parent level in nested functions) are inherited locally, meaning that you can access their values and you can modify them locally (with no repercussions on the parent levels).
If you want an empty variable with the same name of a "global" variable, by design or to be error-proof, you have to define such variable locally with new content or you have to use the Clear() function.
SMath provides also an implicit way to modify parent variables, using the pass-by-reference approach, that is what you are experiencing with F in you screenshot.
Basically, if inside the function you use the assignment operator on a variable passed as function argument, the content of the variable is updated globally.
Here you can grasp the expected behavior: https://smath.com/en-US/cloud/worksheet/rzXN77GB
WroteTrying to make a finite element beam calculator. I've already done this in Python just trying to implement my code in SMath as I was hoping to make a more readable version, but it has been a real struggle. My current struggle is trying to understand why a function I created to override the load matrix with 0s only works as intended once, and then throws a "Variable already defined by value of another type" after that.
Hard to say without a worksheet, because there are variables from outside the function involved.
WroteShouldn't everything in the function be a local variable and be defined fresh every time based off of the arguments?
Nope.
By default "Global" variables (from the canvas, or from a parent level in nested functions) are inherited locally, meaning that you can access their values and you can modify them locally (with no repercussions on the parent levels).
If you want an empty variable with the same name of a "global" variable, by design or to be error-proof, you have to define such variable locally with new content or you have to use the Clear() function.
SMath provides also an implicit way to modify parent variables, using the pass-by-reference approach, that is what you are experiencing with F in you screenshot.
Basically, if inside the function you use the assignment operator on a variable passed as function argument, the content of the variable is updated globally.
Here you can grasp the expected behavior: https://smath.com/en-US/cloud/worksheet/rzXN77GB
Отредактировано 04.06.2025 13:16:44
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
2 пользователям понравился этот пост
#3 Опубликовано: 04.06.2025 16:42:56
Thanks for the reply. I just figured out one reason it's not working for me. So I am ultimately trying to call this function 6 different times with different F vectors and nodes depending on 6 different load combinations. The iteration that failed had an empty load matrix, i.e. just a vector of 0s. When I gave it some values the function would run. Now I just have to figure out why the function gives me a variable error when it sees all 0s? I'm thinking it shouldn't matter what the content of it is, if its overrides 100 with 0 or 0 with a different 0. But it does seem to matter.
1 страниц (3 вхождений)
-
Новые сообщения
-
Нет новых сообщений