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

#1 2012-09-12 12:39:50

jbrownos
New Scratcher
Registered: 2012-09-12
Posts: 3

Making a function like those in other languages

I'm looking into using Scratch as part of a very basic class on programming and computer science, and it provides many of the basic imperative-style elements fairly well.  Despite that, the ability to declare and reference a function is one of the most basic ideas in CS and writing code.  I wanted to ask if there was anything in Scratch that would help students to understand how functions work.

Ideally, I'd like to have some kind of a value (oval) block, which would essentially give whatever the function returns.  The block would hopefully take a variable number of inputs (arguments), in addition to some way of indicating what function to use.  With an oval block, you could either use the returned value or simply ignore it and produce side effects (which is almost exactly like C).  Ideally a function could be made global and could be referenced in any sprite or object available.

I'm kind of guessing this isn't exactly possible the way I'd prefer to do it.  The "broadcast" ability would sort of work, but you'd have to set some variable as the arguments and then set another variable to get a return value.  Unfortunately, that's a rather indirect and difficult way to help a student understand the concepts of how functions work in most languages.  After Scratch, I'll likely go into something like basic Python or maybe even C, and knowing how to use a function is going to be absolutely crucial to learning good programming techniques.

Offline

 

#2 2012-09-12 12:45:18

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Making a function like those in other languages

Hi jbrownos.
Scratch 1.4 does not support the creating of functions, but as you said, the broadcast feature could be used as a workaround.
Scratch 2.0, which will hopefully be released in a few months, will support procedures - i.e. functions that run, but do not return a value. These functions will take parameters. It is also possible to set up a variable to simulate returning a value. It is possible that at some time after that Scratch will support the oval (reporter) functions.
Good luck with your class!

Offline

 

#3 2012-09-12 13:40:51

jbrownos
New Scratcher
Registered: 2012-09-12
Posts: 3

Re: Making a function like those in other languages

Thanks for the reply.  Like I said, I suspected there wasn't any way to do traditional functions right now, but I'm glad to hear they'll be supported in the future.

I'm curious to know exactly how function block would be set up.  It seems like specifying the number of inputs/arguments to accept would be would of the biggest issues.  If 2.0 is similar to the current version, I'm guessing it would be easiest to just have different blocks for each specific number of arguments.  That's kind of limited and dissimilar to traditional programming, so I hope someone comes up with a more elegant solution.

Anyway, thanks again for the information.  I'll try to come up with a way to present the whole "broadcast" idea in a way that leads into other languages.  The most important part is getting students to understand the idea of creating a method to help with a common task.  I'm guessing a simple math formula that gets used in several places might be a good start.  I'll try to post whatever I come up with for getting the idea across effectively.

Offline

 

#4 2012-09-12 13:47:50

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: Making a function like those in other languages

You can read more about procedures in Scratch 2.0 on the Scratch Wikismile  It seems to work pretty well.

You're welcome!

Offline

 

#5 2012-10-25 13:04:38

Pawnfork
Scratcher
Registered: 2011-02-06
Posts: 14

Re: Making a function like those in other languages

While not formally a capability, why not load one or more functions into a sprite, make it invisible and like you said, message with broadcast.  Finding your way back to the right place, not so much fun.

Offline

 

Board footer