< Language Tutorials

SandCastleIcon.png This article has links to websites or programs outside of Scratch and Wikipedia. Remember to stay safe while using the internet, as we cannot guarantee the safety of other websites.

In Python there are functions which are very similar to custom blocks in Scratch. This tutorial will discuss the similarities and differences between the two.

Uses

Both functions and custom blocks are used to hold a group of code. You can then call these funtions in your code just like you can call code from a custom block. Here is an example of code from Scratch and code from Python that prints out "Hello World":

when green flag clicked
helloWorld

define helloWorld
say [Hello World]

def helloWorld():
     print("Hello World")

helloWorld()

Functions and custom blocks are often used for organizing code and storing a group of code that you may need to use more than once in one place. Then you do not have to copy and paste the code over and over again throughout your project. In the example above there's only one block or one line of code making it not as useful to use a function or custom block, but when you get to large groups of code, it becomes very useful!

Function Arguments

Sometimes you need to give a custom block specific information for it to be able to perform its code. In Python you can give functions information too. These are called parameters. For example, if you needed a function or custom block that would say "hello" to a specific person, you would need to give it the person's name. Here is what that would look like in Scratch and in Python:

when green flag clicked
sayHello [John]

define sayHello (person)
say (join[Hello ](join (person) [!]))

def sayHello(person):
     print("Hello " + person + "!")

sayHello(John)

In Scratch you can also give a custom block a boolean value. You can do this in Python too. In Python, you can give a function a boolean value the same way that you can give it any other value (such as a String or number), however in Scratch there is a similar but slightly different way. Here's what they look like:

when green flag clicked
isThisTrue <[1] > [2]>

define isThisTrue <Boolean>
if <Boolean> then
     say [Yes, it is true.]
else
     say [No, it is not true.]

def isThisTrue(Boolean):
     if Boolean:
          print("Yes, it is true.")
     else:
          print("No, it is not true.")

isThisTrue(1 > 2)

Returning A Value

In Python functions can return values. For example, if you had a function that would return the factorial of a number, and you needed to print out the factorial of five multiplied by two, you could set the answer equal to the function call multiplied by two. Here's what that would look like:

def factorial(num):
     index = num - 1
     factorial = num
     while index > 0:
          factorial *= index
          index -= 1
     return factorial

print(factorial(5) * 2)

This however cannot be done in Scratch. Instead you would need to keep track of the answer using a variable, call the function, and then say what the variable multiplied by two is. Here's what that would look like:

when green flag clicked
factorial [5]
say ((factorial) * [2])

define factorial (n)
if <(n) > [0]> then
    factorial ((n) - [1])
    set [return v] to ((n) * (return))
else
    set [return v] to [1]
end

Built-In Functions

Document.png Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (August 2020)

In Python there are built in functions which are functions that have already been coded. Some of these built in functions are similar to blocks in Scratch. Here are some of the built in functions compared to Scratch blocks that are similar.[1]


abs()

Python has a function called abs() which gives the absolute value of a number. Here's an example of how it could be used to find the absolute value of -10:

abs(-10)

In Scratch the () of () block can be used to find the absolute value of a number when the abs option is selected. Here's what that looks like:

[abs v] of [-10]

The other () of () blocks require you to import the math package, for example, this is the [sqrt v] of () block:

import math
math.sqrt()

input()

There is a function in Python that allows you to get user input through the console. It's called the input function. It can take an argument which is what it will ask or tell you to enter. Here's an example of how it can be used to get a user's name:

input("Enter your name: ")

This is very similar to the ask () and wait block. Here's what the program above could look like in Scratch:

when green flag clicked
ask [Enter your name: ] and wait

len()

In Python there is a length function. It can be used to find the length of a string. Here's an example of how the length function would be used to find the length of the String "Hello World":

len("Hello World")

To find the length of a string in Scratch, the length of () block would be used. Here's what that would look like:

length of [Hello World]

The length of a list can also be found using the length function in Python. Here's what that would look like:

fruit = ["apples", "bananas", "oranges", "pineapple", "watermelon"]
len(fruit)

In Scratch there is a separate block for finding the length of a list. It's also called length of (), but it's specifically for lists. Here's what it looks like:

length of [fruit v]

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.