0% found this document useful (0 votes)
26 views3 pages

12 Functions 90dcbe9f5

The document discusses functions in programming. It explains that functions are blocks of code that perform a specific task and can be reused. Functions make code more organized and prevent duplication. The document provides examples of defining and using functions in Python and JavaScript. It also shows how to correctly define a function with the proper syntax and indentation.

Uploaded by

sonofameme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views3 pages

12 Functions 90dcbe9f5

The document discusses functions in programming. It explains that functions are blocks of code that perform a specific task and can be reused. Functions make code more organized and prevent duplication. The document provides examples of defining and using functions in Python and JavaScript. It also shows how to correctly define a function with the proper syntax and indentation.

Uploaded by

sonofameme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Subject: Functions Blocks and Button icons to be used in the training about this subject

Overview
In this lesson we will learn about what functions are, how a function is Command Explanation
defined and how it is used. The function which our panda character will use for
collecting the capsules.
Functions are structures which have certain functions in programming and The function which our panda character will use for
which we use again and again. We will learn how we can shorten the long collecting the capsules
commands which are required for solutions in task scenes and how we can The function which will ensure our panda character
facilitate the solutions using functions. Functions are code parts which receive a builds a bridge to let it pass through the stream by
using the bamboo it collected.
certain input and produce outputs. It is written once and then it is used in many
places. Functions are important when it comes to object-oriented programming.
Block Explanation
Activity You can recall a function by using its name when
necessary and use it again and again by writing the
Let's try to understand what functions are by drawing an analogy from real recurring actions during the solution of a task with the
life. For example, we buy a fruit press and whenever we desire fruit juice we use appropriate order for the solution within the function
this tool. That is to say, in fact the mission and function of this tool is to prepare you defined.
juice. The most important point to be considered while
defining the function should be the spelling of the
For example, the mission of the the most used print() function is to print
function name. “Def” comes from the word define
the values we send inside it on the screen. This function is written once by Python and it is used in the meaning of defining something.
developers and we use this function in different parts of our programmes again and The following function name to be written should
again. This is exactly what the intended purpose of functions are. Functions are start with a lowercase letter, but if it is a two-word
defined once and we use them in programmes when we need them. Besides, name, the first letter of the second word must be
functions prevent code duplication and our codes stay more organised. capitalized, without spacing. Also in Javascript
language the word function is used.
Commands to be used
Sample usage (Python):
def (Python), function (Javascript) commands which they will use to complete the
def getCapsule():
tasks and the commands which were learnt previously.
Sample usage (Javascript):
WARNING: You will see that in block mode the screen will split into two sides. Our
function getCapsule(){
predefined function will appear at the bottom. In block mode you define the codes
}
to run within the function by dragging and dropping the required blocks under this
function and at the top. you can use and run this function in the scene solution.

codementum.com
BLOCK Erroneous writing forms
Usage Explanation
Python
Function name

The commands to run


within the function

Why is it wrong? Why is it wrong? Correct form:


The commands which are When function is defined, Our code is correct because
required to be written spacing should be made attention is paid to spacing
within function should be between words. It can also which is required to be made
written after spacing with be noticed that there is between expressions and the
PYTHON the tab key. incorrect writing due to the indentation in the
absence of colour change in statements within function.
Usage Explanation “def” expression.

Function name
JAVASCRIPT
Usage Explanation
The commands to run
within the function Function name
çalışacak komutlar
Usage place of the
function The commands to run
within the function

Usage place of the


function

codementum.com
Sample Solution: Scene 123

Algorithm Block Python Javascript

 Start def getCapsule(): function getCapsule(){


 Declare a function named forward(5) forward(5)
“getCapsule”. turn(right) turn(right)
 Within the“getCapsule” function repeat 3: }
 Move 5 squares forward getCapsule() repeat(3){
 Turn right getCapsule()
 3 times within Repeat loop }
 Recall “getCapsule”
function and use it.

Sample Solution: Scene 126

Algorithm Block Python Javascript

 Start def getAcross(): function getAcross(){


 Declare a function named forward(3) forward(3)
“getAcross”. build() build()
 Within the“getAcross” function forward(2) forward(2)
 Move 3 squares forward turn(left) turn(left)
 build a bridge by using repeat 2: }
“build” function getAcross() repeat(2){
 Move two squares forward getAcross()
 Turn left }
 2 times within Repeat loop
 Recall “getAcross”
function and use it.

codementum.com

You might also like