define questions
06-21-2017, 12:31 PM
Post: #1
 webmasterpdx Senior Member Posts: 541 Joined: Sep 2015
define questions
I'm gradually figuring out how to program the HP Prime. I'm not new to programming calculators and have 35 years experience as a software engineer, but I'm new to the HP prime. I keep running into CAS vs non-cas issues. Well I have a new one.
Before I describe this problem, let me say, yes, I know I can avoid using the define key altogether and create a program instead. I just want to know how to do it using the define key.
I have a program and I'm using 2 functions I programmed using the Define key. These are:
INC which is defined as X:=X+1;
INC2 which is defined as X:=X+2;

Now when I run my program it fails and when I debug it, it doesn't seem to increment my local variable n when I pass it in using INC(n);

So, I tried to run this from the CAS view using 0->a and then type INC(a), which gives me argument errors, so I try it from the home view.
Here, INC(a) shows 1, but when I run it again, it shows 1 again, instead of 2.
Then I tried INC2(a), and it shows 2, but when I try to run it again, it shows 2 again instead of 4. Why is this?
What am I missing?
It's starting to look to me like Defined functions are worthless. I thought they were similar to inline macros in C, rather than a compiled C function, which is what I'd see an EXPORTed function as being analogous to.
06-21-2017, 02:04 PM
Post: #2
 Tim Wessman Senior Member Posts: 2,231 Joined: Dec 2013
RE: define questions
Your functions are being defined as such in essence:

auto INC(auto X)
{
return X=X+1; //global X is masked, only local variable is modified
}

There is no reference being passed here, and X is local to the function. It is not a macro in any way shape or form. You are passing the value of something, and returning the value.

Now if you are wanting to increment the "global" X value, defining your function should be done like so instead:

X:=N+1;

Then uncheck the X as an input value. That would give you:

auto INC(auto N)
{
return X=N+1; //global N is masked, global X real variable modified
}

Defined functions through the interface are meant to be a way for new users to define algebraic functions for use during calculations primarily. Honestly, I'm not sure why you are seeing the error message when calling it from the CAS. We'll check into this. However, the CAS is a functional runtime language and defining a function in the CAS is directly done by just typing this on the command line:

myfunc(a):=begin a:=a+1; end;

TW

Although I work for the HP calculator group, the views and opinions I post here are my own.
06-21-2017, 02:14 PM (This post was last modified: 06-21-2017 02:16 PM by DrD.)
Post: #3
 DrD Senior Member Posts: 1,124 Joined: Feb 2014
RE: define questions
This works on my emulator:

[Shift][Define] (Name) inc2 (Function) X+2 (and uppercase Variable X is checked). [OK]

Then: (At the command entry line):

(In your case) N:=2, and inc2(N) ==> 4

Case matters! In general, UPPERCASE is used for HOME content, and lowercase is used for CAS content. If you are just getting started with hp Prime, you are likely to be disillusioned by jumping to conclusions, such as Define being worthless. A jet aircraft is worthless, by that standard, if you are asked to fly one without any training or experience.
06-21-2017, 03:17 PM
Post: #4
 webmasterpdx Senior Member Posts: 541 Joined: Sep 2015
RE: define questions
You defined INC2 as X+2. That would mean I'd have to use it as in n:=INC2(n);
I see what you are saying....n is local to INC2, so I'd have to use it this way. I wanted to be able to increment a variable without having to type the variable twice, as in x:=x+1;
....kind of like ++x; in C.
I might as well type in n:=n+2 rather than n:=INC2(n);
I can create a regular program to do it. It would have been nice to define a function though (more elegant), where I can whip out quickly common functions that I use a lot. I've had a thought though, (possibly using 'n' as an argument, which "kind" of works like passing in a pointer)....need to experiment a little. I'll post if I can get it to work.
Thx
-D
06-21-2017, 09:19 PM (This post was last modified: 06-21-2017 09:20 PM by DrD.)
Post: #5
 DrD Senior Member Posts: 1,124 Joined: Feb 2014
RE: define questions
Suppose you had 100 Volts show up across a 50 Ohm load, and you had the function, "pwr" defined as E^2/R. Then by entering pwr(100,50), you might expect 200 watts to be dissipated.

WRT units, you can begin to see keystroke efficiency by this simple definition. If you were tracking satellites, performing buoyancy computations, kinematics, or many other aspects of the real world, (and your calculations involved a lot of repetition), the ROI improves. At some point, more robust programming is advantageous.

Your greater point that the Define feature was worthless, is much the same as girls have been telling boys for years, "It's not what you have, it's what you do with it!" (One can only presume they were talking about finances ...)
06-21-2017, 11:07 PM
Post: #6
 Didier Lachieze Senior Member Posts: 1,169 Joined: Dec 2013
RE: define questions
(06-21-2017 03:17 PM)webmasterpdx Wrote:  I've had a thought though, (possibly using 'n' as an argument, which "kind" of works like passing in a pointer)....need to experiment a little. I'll post if I can get it to work.

If you define INC2 as: EXPR(REPLACE("X:=X+2","X",Y)) with Y checked then the following program will increment the local variable m by 2 and return 3:
Code:
EXPORT TST2() BEGIN LOCAL m;   m:=1;   INC2("m");   RETURN m; END;
06-22-2017, 02:12 AM
Post: #7
 AlexFekken Member Posts: 151 Joined: May 2016
RE: define questions
My C is a bit rusty, but I am sure even in C you would get the same behaviour if you create functions with a "standard" 'int x' parameter. You would need to pass the argument by reference (define the parameter as 'int &x') to increment it. I don't think you can do that on the Prime, but I am happy to be corrected.

If I had a guess then I'd say perhaps you have a Java background where everything is an object and passed by reference. :-)
06-22-2017, 12:05 PM
Post: #8
 webmasterpdx Senior Member Posts: 541 Joined: Sep 2015
RE: define questions
Nope....though I've used Java. I'm aware of the references in C. I thought the define was like a macro functionality, but it isn't. It's more like a one line program....which makes me wonder at the point of it...having 2 ways to write programs that act similarly.
Didier has it right. Based on everything I've examined, the only way to pass an argument by reference is as a string, but that involves string parsing in real time when all I wanted to do was increment a variable. So, I'd only use x:=x+1; in my program as speed is important since the application involves a lot of calculations (takes a few minutes to run even on the prime). i.e. parsing a string in real time is too slow.

What the prime does need is either a way to pass arguments by reference/pointer or a macro capability.
Thanks for all the responses.
-Donald
06-26-2017, 06:13 AM
Post: #9
 cyrille de brébisson Senior Member Posts: 974 Joined: Dec 2013
RE: define questions
Hello,

You are correct, DEFINEs (internally) ARE programs, very simplified ones, but programs nevertheless. They are programs because that is what makes them the easiest to implement.

Why do we have defines while there is a perfectly good programming environment?

Because it is much easier for a non-programmer/beginner to use the define environment than the programming one!

One of the problem that we have to solve with Prime is that it is used by a wide variety of persons, with a wide variety of knowledge levels and center of interest.

The programming system, for example, allows you to migrate easily from novice to advanced users, slowly learning better and better programming technics. At the begining using home global variables for everything and then slowly moving up to much cleaner programs for example...

But this comes at a cost. For example some things are there twice, in various forms to make it easier for various persons... Cleanness of code is not enforced...

Cyrille

Although I work for the HP calculator group, the views and opinions I post here are my own. I do not speak for HP.
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)