<< Chapter < Page Chapter >> Page >

Passing parameters

This process of providing information to a function when you call it is commonly referred to as passing parameters to the function. For the square-root function, you need to pass a parameter whose value is the value ofthe number for which you need the square root.

You call the built-in square root function in Scratch 2.0 by dragging the bottom block shown in Image B into the programming panel and selecting sqrt in the pull down list. You pass the parameter by entering a literal value or dragging a variable block into the white areathat contains the 9 character in Image B .

Returning values

A function or method will usually

  • Perform an action,
  • Send back an answer
  • Some combination of the two

Performing an action

You might think of performing an action as something like causing a Scratch sprite to move to the right and change color.

None of the Scratch 2.0 mathematical functions shown in Image C perform an action. One of the purposes of this module is to teach you how to create customblocks that do perform an action.

Sending back an answer

On the other hand, a function that is designed to calculate the square root of a number needs to be able to send the square-root value back to the programthat called the function. After all, it wouldn't be very useful if the function calculated the square root and then kept it a secret. The process of sendingback an answer is commonly referred to as returning a value .

All of the Scratch mathematical functions shown in Image C return a value.

It appears that there is no straightforward way for you to cause a custom block that you design to return a value. However, I will show you a workaroundfor that situation later.

Creating your own custom blocks

The best explanation that I have found for the mechanics of creating a custom block is contained in the document titled How Do I Use the New Blocks? on the MIT website. Go to that webpage and scroll down to the section titled Make Your Own Blocks . Follow the instructions that you find there to create your own custom blocks.

Sample programs

With that as an introduction, this section will present and explain two Scratch 2.0 programs built around custom blocks.

CustomBlockTest01

This is the simplest custom block that I could come up with that:

  • Receives an incoming parameter
  • Uses the value of that parameter to compute and return a value

This program contains the two scripts and a variable shown in Image D . These scripts are tied to the cat sprite.

Image d. program code for customblocktest01.

Missing image.
Image D. Program code for CustomBlockTest01.

A global variable

The variable named var shown on the left in Image D is a global variable. That means that it is accessible by any code in the program.

The custom block

The custom block named test is shown in the bottom script in Image D . When called, this block receives an incoming numeric parameter known locally as number1 . The code in the block multiplies that value by a factor of 2 and stores the resulting product in the variable named var , making it available to the code that called the custom block.

Questions & Answers

summarize halerambos & holbon
David Reply
the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
what is Industrialisation
Selekeye Reply
industrialization
Angelo
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Teaching beginners to code. OpenStax CNX. May 27, 2013 Download for free at http://cnx.org/content/col11498/1.20
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Teaching beginners to code' conversation and receive update notifications?

Ask