Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions and Procedures

Similar presentations


Presentation on theme: "Functions and Procedures"— Presentation transcript:

1 Functions and Procedures
In today’s lesson we will look at: what functions and procedures are the difference between the two when we might want to use one creating functions and procedures in Just Basic

2 Functions and Procedures
Functions and procedures are small sections of code used to perform a specific task. They are used for two reasons: they can be used to avoid repetition of commands within the program careful use of functions and procedures helps to define a logical structure for your program by breaking it down into a number of smaller modules – i.e. they are the building blocks of larger programs

3 Functions and Procedures
Are functions and procedures the same? Not usually. Generally... functions return a value – i.e. they calculate an answer and send it back procedures don’t return a value – they just perform an action However, in some programming languages there are only functions, and procedures are seen as a special case of a function, just as a square is a special type of rectangle

4 When Are Functions Useful?
You’ve probably used a function in a spreadsheet – e.g. =sum() returns the total value of the numbers in the range. We don’t need to write all of the code to do that. Blocks in Scratch are procedures – see how they are to simplify the Simple Spirograph program Functions and procedures can be used where you want to repeat the same action throughout your program without repeating all of the code.

5 Functions in Just Basic
Both functions and procedures are defined at the bottom of your program. Functions are called using their name, e.g. print double(3) end function double(x) double = x*2 end function The main program finishes here Use the name of the function to return the value

6 Procedures in Just Basic
Procedures in Just Basic are called subroutines Procedures are called using the call keyword and their name, e.g. call hello end sub hello print "Hello" end sub The main program finishes here Note that there is no return value – the procedure just finishes

7 Be Careful With Variables!
You can create and use variables inside a function or procedure, but these are called local variables Local variables: can’t be accessed by any other part of the program have their values reset each time the function or procedure is called Values passed to functions and procedures don’t have their values changed unless preceded by byref in the definition, e.g. function double(byref x)


Download ppt "Functions and Procedures"

Similar presentations


Ads by Google