BookmarkSubscribe
Choose Language Hide Translation Bar
Highlighted
Yngeinstn
Community Trekker

Expression vs. Functions

I been struggling to understand the differences. It looks to me, at least on the surface that they accomplish the same thing but seeing how you get to choose there is a difference somewhere. If you fine folks wouldn't mind giving me the dumb downed explination with maye an example or two i would greatly appreciate it.

 

D

1 ACCEPTED SOLUTION

Accepted Solutions
msharp
Super User

Re: Expression vs. Functions

My take: Functions are for code that will be recurring.  Expressions are for inserting code into other code.

 

Here's a simplified example:

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

onewayFunc = function({dt,Yaxis, Xaxis}, {Default Local},
	plot = dt << Oneway(
		Y( eval(Yaxis) ),
		X( eval(Xaxis) )
	);
	return(plot);
);
Yaxis = expr({Column("height"), Column("weight")});
Xaxis = expr(Column("sex"));
onewayFunc(dt,Yaxis,Xaxis);

This code could be accomplished without expressions, but using it as an example.  In general I would say always use functions, until you meet an edge case where an expression will get the job done.

 

Expressions also have several useful helper functions making them useful for arithmatic and alegebra:

Names Default To Here( 1 );
Simplify Expr( Expr( 2 * 3 * a + b * (a + 3 - c) - a * b ) );
//6 * a + 3 * b + -1 * b * c
4 REPLIES 4
msharp
Super User

Re: Expression vs. Functions

My take: Functions are for code that will be recurring.  Expressions are for inserting code into other code.

 

Here's a simplified example:

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

onewayFunc = function({dt,Yaxis, Xaxis}, {Default Local},
	plot = dt << Oneway(
		Y( eval(Yaxis) ),
		X( eval(Xaxis) )
	);
	return(plot);
);
Yaxis = expr({Column("height"), Column("weight")});
Xaxis = expr(Column("sex"));
onewayFunc(dt,Yaxis,Xaxis);

This code could be accomplished without expressions, but using it as an example.  In general I would say always use functions, until you meet an edge case where an expression will get the job done.

 

Expressions also have several useful helper functions making them useful for arithmatic and alegebra:

Names Default To Here( 1 );
Simplify Expr( Expr( 2 * 3 * a + b * (a + 3 - c) - a * b ) );
//6 * a + 3 * b + -1 * b * c
Yngeinstn
Community Trekker

Re: Expression vs. Functions

Thank you for the information. I have 6+ include() scripts that are nothing but Expressions.. I am going to try the function now..

0 Kudos

Re: Expression vs. Functions

In addition to using the dynamic nature of expressions, I see expressions as another way to make a subroutine. I could use a function or an expression as a subroutine. I prefer an expression when I want to delay evaluation, don't need parameters, and don't need a result returned. I prefer a user function when I need parameters or a return result.

Learn it once, use it forever!
0 Kudos
Yngeinstn
Community Trekker

Re: Expression vs. Functions

Thank you Mark,

 

I started a new topic based on the information you were able to provide. Functions are definitely what i needed to understand as it significantly reduces the lines of code in my data pull script.

 

Here is the link https://community.jmp.com/t5/Discussions/How-to-loop-using-a-function/m-p/74841

0 Kudos