This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2007-05-20 08:39:01

jsimone
Scratcher
Registered: 2007-05-20
Posts: 28

Passing arguments to scripts

I would like to request the ability to pass arguments to script invocations.

This is obviously an advanced programming construct, but it would be great to be able to define a chunk of script code once, say in the stage, such that it belongs to (applies to) all sprites and then be able to reuse it. 

Without this, it makes the code messy having to copy the same chunk of code from sprite to sprite and change a block or two with some different numbers.

Many, many thanks!!!

Offline

 

#2 2007-05-20 10:46:46

Roberth
Scratcher
Registered: 2007-05-15
Posts: 46

Re: Passing arguments to scripts

There are "global" variables that you can assign values to, and use some of them for parameter passing.  I know this is ugly and is prone to introduce bugs if you have multiple "subroutines", but it is one way to pass parameters.  And then you can use the "Broadcast <message> and wait" as a sort of subroutine invokation device.  This has been done on several other Scratch scripts, including some of the examples that are installed with Scratch.

So, yes, you can have just one block of code that can be invoked from multiple sprites and locations throughout a project, but you do have to get a little bit creative.

Offline

 

#3 2007-05-20 20:37:14

jsimone
Scratcher
Registered: 2007-05-20
Posts: 28

Re: Passing arguments to scripts

Robert,
In order for Scratch to be effective in teaching kids programming, constructs such as Logo's ability to create named procedures with multiple parameters is essential.  In Logo, which also came out of MIT, you could simple create a "to square :sideLength" that creates a square with the side length passed in.  This in turn can be called from other "to" code creating higher order functionality.

Scratch team:
Please consider functionality like this for a future release.

Thanks!

Offline

 

#4 2007-05-21 00:25:58

Roberth
Scratcher
Registered: 2007-05-15
Posts: 46

Re: Passing arguments to scripts

I would have to agree that a formal procedure call would be incredibly nice to have.  I'm just trying to suggest a current "hack" to attempt something along this line of thought.

I'm fighting one of those "procedures" right now in one of my scripts.

Offline

 

#5 2007-06-01 20:25:48

wjcarpenter
Scratcher
Registered: 2007-05-29
Posts: 5

Re: Passing arguments to scripts

Roberth wrote:

I would have to agree that a formal procedure call would be incredibly nice to have.

Pragmatically, it's more than nice-to-have.  Without it, we're teaching kids to do one of the Great Evils of programming: repeated cut-and-paste of code.  Unfortunately, I know many professional programmers who think that's a fine way to do business.

Offline

 

#6 2007-06-01 23:25:55

rivendell
Scratcher
Registered: 2007-05-29
Posts: 24

Re: Passing arguments to scripts

Agreed, without some sort of function or subroutine capability it's severely limited in the scope of teaching "real" programming.

You could simplify things a bit, in that every routine called would use the sprite it was called from to operate on (a psuedo-oo kind of thing).

Right now I see lots of projects (including mine) with numerous sprites all with the same code cut and pasted.

Offline

 

#7 2007-06-02 13:07:43

wjcarpenter
Scratcher
Registered: 2007-05-29
Posts: 5

Re: Passing arguments to scripts

An easy start of subroutines could be a macro-like feature where an arbitrary group of blocks (maybe a complete script?) could be put into a graphical wrapper and reused as a whole.  I'm thinking of a "define" block something like the "when [] key pressed" control, but it would instead be "define block named []".  A companion "use" block would be shaped like most of the blue motion blocks (notch at the top, bump at the bottom) with a label like "use block named []".

Just like variables, the define/use blocks could be scoped to a sprite or scoped globally.


To get formal parameter stuff, I think you need a new kind of variable (a local variable, not scoped to a sprite or globally).  Still, I think it could reasonably done while still maintaining the simple Scratch graphical paradigm

Offline

 

#8 2007-06-02 14:36:04

DrJim
Scratcher
Registered: 2007-05-26
Posts: 100+

Re: Passing arguments to scripts

wjcarpenter wrote:

Without it, we're teaching kids to do one of the Great Evils of programming ....I know many professional programmers who think that's a fine way to do business.

Lighten up - computers are tools - not religious icons.  The really dangerous programmers are the ones who think orthodoxy is more important than actually creating something that works.

Offline

 

#9 2007-06-02 21:46:42

wjcarpenter
Scratcher
Registered: 2007-05-29
Posts: 5

Re: Passing arguments to scripts

DrJim wrote:

The really dangerous programmers are ....

Sorry, did you mean to be talking about me?  I'm not sure since nobody has ever called me a "dangerous" programmer (except maybe the test team :-).

Offline

 

#10 2007-06-03 11:34:57

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Passing arguments to scripts

I found Dr. Jim's comment a little off the mark.  "Something that works" is often not good enough---it has to continue to work despite changes in the underlying operating system and compilers, and to be maintainable.  Without procedures and comments, scratch programs are limited to rather toy examples---fun to play with, but not as good a teaching tool as it could be.

Offline

 

#11 2007-06-03 14:23:12

DrJim
Scratcher
Registered: 2007-05-26
Posts: 100+

Re: Passing arguments to scripts

kevin_karplus wrote:

"Something that works" is often not good enough---

It is necessary (!) - and occasionally sufficient. ( Not very often, of course.)

My point, however, was more to suggest that Scratch is more a way to get kids interested in general programming concepts - not to actually turn them into hard core programmers.

P.S. - In most test teams I've been on - "dangerous programmer" is redundant, at least after the first couple of days. :-)

Last edited by DrJim (2007-06-03 14:24:03)

Offline

 

Board footer