paulh
New Member
Posts: 7
|
Post by paulh on Aug 2, 2020 23:39:00 GMT
Hi,
Am a newbie and have a question. I want to define a function such as x^3+x^2 +1 and use that function in my program. Is there a command such as DEF function that will define that function so I can evaluate it for certain values?
Thanks, Paul
|
|
|
Post by carlgundel on Aug 3, 2020 0:33:02 GMT
Hi, Am a newbie and have a question. I want to define a function such as x^3+x^2 +1 and use that function in my program. Is there a command such as DEF function that will define that function so I can evaluate it for certain values? Thanks, Paul Sure. print myFn(5) end
function myFn(x) myFn = x^3+x^2+1 end function
|
|
|
Post by Rod on Aug 3, 2020 12:19:59 GMT
Generally speaking we don’t need to define functions or define variables and variable types in Just BASIC because running the program is a two step process. Firstly your code is compiled, that takes care of all the definitions. Variable types are simplified down to integer, float or string. You will recognise a float when you see a . within a numeric variable.
After compiling the code is then interpreted line by line and run.
All seamless and behind the scene thus simplifying coding with Just BASIC.
|
|
paulh
New Member
Posts: 7
|
Post by paulh on Aug 3, 2020 18:01:00 GMT
Guys,
Thanks for the tips. Just (no pun intended) what I needed.
Paul
|
|
|
Post by carlgundel on Aug 4, 2020 16:39:20 GMT
Generally speaking we don’t need to define functions or define variables and variable types in Just BASIC because running the program is a two step process. Firstly your code is compiled, that takes care of all the definitions. Variable types are simplified down to integer, float or string. You will recognise a float when you see a . within a numeric variable. After compiling the code is then interpreted line by line and run. All seamless and behind the scene thus simplifying coding with Just BASIC. That's an important point. The function declaration does not have to be above the code that uses the function. In Liberty BASIC, and indeed in most languages that support user functions and subroutines, the code that defines functions is allowed to be anywhere in the program code.
|
|
paulh
New Member
Posts: 7
|
Post by paulh on Aug 7, 2020 21:49:41 GMT
Again, thanks for your help. I am not clear on one thing regarding this.
You gave me as an example:
print myFn(5) end
function myFn(x) myFn = x^3+x^2+1 end function
I am wondering if it is possible to enter a function in some form on an INPUT statement at the beginning of the program instead of having it written in the program itself as in the example x^3+x^2+1? I
|
|
paulh
New Member
Posts: 7
|
Post by paulh on Aug 7, 2020 21:53:13 GMT
I guess I would want something like:
Input <some function> print myFn(5) end
function myFn(x) myFn = <some function> end function
Thanks, Paul
|
|
|
Post by B+ on Aug 8, 2020 0:15:26 GMT
I guess I would want something like: Input <some function> print myFn(5) end function myFn(x) myFn = <some function> end function Thanks, Paul Been there, done that! Create a txt file with formulas and variables and their current value. The app allows you to add a formula, AKA Function, variable or change variable values see here: justbasiccom.proboards.com/thread/482/handy-formula-saverFrankly it's easier to edit the text file with formulas and values and just run the app to evaluate the formula at the current variable settings. Just follow the example txt file given in the thread.
|
|
|
Post by Rod on Aug 8, 2020 7:57:52 GMT
I am getting confused by function and formula. Functions are built into the program and can be reused, they are defined at the start by writing them into the code which is compiled and run. We can't introduce a new function after that.
Are you trying to get a maths formula to compute or are you trying to add new functions to the program flow?
|
|
|
Post by tsh73 on Aug 8, 2020 8:45:48 GMT
I believe he really looks for EVAL www.libertybasicuniversity.com/lb4help/EVAL.htmJB does not have EVAL function, but its bigger (paid, could be tried as unregistered shareware) cousin LibertyBASIC has. (link on forum homepage, top right) Of course folks get creative and invent theirs own EVAL-like things But sometimes (code from that LB helpfile topic) 'ask for an expression input "Type a numeric expression>"; code$ answer = eval(code$) print answer *this* clearly looks like reason to get LB.
|
|
|
Post by tenochtitlanuk on Aug 8, 2020 10:16:08 GMT
Yes- move up to LB and have eval() available! global expr$
print "My function of x ( eg x^3 +x^2 +1) " input expr$
print " Evaluating "; expr$; " with x =5 yields "; myFn( 5) end
function myFn( x) myFn =eval( expr$) end function
Outputs.. My function of x ( eg x^3 +x^2 +1) ?x^3 +x^2 +1 Evaluating x^3 +x^2 +1 with x =5 yields 151
Of course it gets more fun if you evaluate over a range of x..
global expr$
print "My function of x ( eg x^3 +x^2 +1) " input expr$
for x =0 to 10 print " Evaluating "; expr$; " with x ="; x; " yields "; myFn( x) next x
end
function myFn( x) myFn =eval( expr$) end function
My function of x ( eg x^3 +x^2 +1) ?x^3 +x^2 +1 Evaluating x^3 +x^2 +1 with x =0 yields 1 Evaluating x^3 +x^2 +1 with x =1 yields 3 Evaluating x^3 +x^2 +1 with x =2 yields 13 Evaluating x^3 +x^2 +1 with x =3 yields 37 Evaluating x^3 +x^2 +1 with x =4 yields 81 Evaluating x^3 +x^2 +1 with x =5 yields 151 Evaluating x^3 +x^2 +1 with x =6 yields 253 Evaluating x^3 +x^2 +1 with x =7 yields 393 Evaluating x^3 +x^2 +1 with x =8 yields 577 Evaluating x^3 +x^2 +1 with x =9 yields 811 Evaluating x^3 +x^2 +1 with x =10 yields 1101
And even more fun if you go for a graphical output. And then you'll want to auto-scale so it 'fills the paper'. Before you know it, a full graphical calculater! You might look at www.diga.me.uk/indexDemo.html for some of my graphing examples. LB is available FREE with a nag screen. As well as eval() it'll give you access to the fabulous 'lsn' lesson addition in LB. Download my lsn files and load/run in LB ( remember to make sure the file selecter is set to show files with the '.lsn' extension.
PS One LB 'gotcha'. Don't copy/paste into an input prompt- it doesn't work!
|
|
|
Post by tenochtitlanuk on Aug 8, 2020 11:59:46 GMT
.. so, fun continued, ...
print " My function of x ( eg x^3 +x^2 +1) " input expr$
WindowWidth =550 WindowHeight =550
open "Graphed function" for graphics_nsb as #wg
#wg "trapclose quit" #wg "down" #wg "line 10 250 510 250" #wg "line 260 10 260 510"
for x =-10 to 10 step 0.02 #wg "set "; int( 250 +25 *x); " "; int( 250 -eval( expr$)) next x
#wg "getbmp scr 1 1 511 511" bmpsave "scr", "demoEvalGraph.bmp"
wait
sub quit h$ close #wg end end sub
|
|
|
Post by B+ on Aug 8, 2020 16:39:09 GMT
Get this, get that... Here is paulh example run with the code from link I gave:
Instructions:
Use menu item #3 paulh = x^3 + x^2 + 1
Use menu item #1 to set x x = 5
Use menu item #5 to evaluate the function at x = 5
Here is Output:
Formula Evaluation Menu
Current Formula: paulh = x^3 + x^2 + 1
1 for setting / resetting a variable and value 2 for getting a list of variables and values 3 for settimg a new formula 4 for selecting a formula from list saved 5 for Evaluating Current Formula with current variable values 6 for saving all current variables = values and formulas 7 for quitting
Please enter your choice number 5 x^3 + x^2 + 1 paulh = 151
Press enter to continue...
JUST BASIC did this!!!
This code needs to show variable values too, I guess, above x was set with "x = 5" using menu item 1.
And sure, defining a function has a technical meaning as demo'd by Carl but I don't think Paul was being technical.
|
|