0% found this document useful (0 votes)
0 views

[AP CSP] [Introduction to Programming (with Karel)] [04] Functions in Karel

Functions in Karel are essential for teaching the computer new commands, breaking programs into smaller parts, and improving code readability. Proper naming conventions for functions are crucial, requiring descriptive names that start with a letter and use lowerCamelCase. The document also distinguishes between defining a function, which teaches Karel a new command, and calling a function, which executes that command.

Uploaded by

salemalsalem2027
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

[AP CSP] [Introduction to Programming (with Karel)] [04] Functions in Karel

Functions in Karel are essential for teaching the computer new commands, breaking programs into smaller parts, and improving code readability. Proper naming conventions for functions are crucial, requiring descriptive names that start with a letter and use lowerCamelCase. The document also distinguishes between defining a function, which teaches Karel a new command, and calling a function, which executes that command.

Uploaded by

salemalsalem2027
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Functions in Karel

What is a function?
A function is a way to teach Karel a new word
Why use functions?
Functions allow us to

1. Break our program into smaller parts


2. Make the program easier to understand
3. Avoid repeating code over and over
Why use functions?
Functions allow us to Functions simplify the problem
solving process

1. Break our program into smaller parts


2. Make the program easier to understand
3. Avoid repeating code over and over
Functions help others
understand our code, allowing
us to code with others!
Programming is really
just teaching the
Functions are computer to do new
things.
one of the most
important parts That’s what functions
are!
of
Functions make it easier
programming! for us to program, and
easier for others to read
our programs
Functions are
All programming
one of the most languages let you write
functions
important parts
of Let’s take a look at
writing functions in Karel
programming!
General Function
function myFunction() {

/* code goes here */


}
Naming is Crucial!
function buildPyramid() {

/* code goes here */


}

From this name it is clear


what the function does
Naming Your Functions
● Name should start with a letter, and cannot have any
spaces
● Every new word in the name starts with an uppercase
letter: lowerCamelCase
● The name should describe what this function does
● The name should start with an action verb, and sound
like a command
Naming Your Functions - Examples
buildTower() Good!

spinTwice() Good!

buildtower() Bad - Should have a capital T

5moves() Bad - Needs to start with a letter

tower() Bad - Not an action / command

blahblah() Bad - Not descriptive


Naming Your Functions - Examples
buildTower() Good!

spinTwice() Good!

buildTower() Good!

5moves() Bad - Needs to start with a letter

tower() Bad - Not an action / command

blahblah() Bad - Not descriptive


Naming Your Functions - Examples
buildTower() Good!

spinTwice() Good!

buildTower() Good!

move5Times() Good!

tower() Bad - Not an action / command

blahblah() Bad - Not descriptive


Naming Your Functions - Examples
buildTower() Good!

spinTwice() Good!

buildTower() Good!

move5Times() Good!

buildTower() Good!

blahblah() Bad - Not descriptive


Naming Your Functions - Examples
buildTower() Good!

spinTwice() Good!

buildTower() Good!

move5Times() Good!

buildTower() Good!

take4Balls() Good!
Defining a Function vs. Calling a Function

Defining a function:
Teaching Karel the new word

Calling a function:
Actually getting Karel to do the command
Defining a Function vs. Calling a Function

Defining a function:
Writing out the instructions for this new action

Calling a function:
Actually causing the action to happen
Defining turnAround()
function turnAround() {

turnLeft();

turnLeft();

This teaches Karel the new word


Calling turnAround()
turnAround();

This tells Karel to actually do the


action
Calling turnAround()
turnAround();

move();

move();

turnLeft();
Calling turnAround()
turnAround();

move();

move();

turnLeft();

turnAround();
This tells Karel to do the action
again!
Defining turnAround()
function turnAround() {

turnLeft();

turnLeft();

This teaches Karel the new word


Let’s go write a function!

You might also like