1 / 17

ICS 3UI – Introduction to Computer Science

ICS 3UI – Introduction to Computer Science. Sub Programs Rationale: allows division of large programming tasks aides debugging simplifies code so that humans can understand it allows code re-use Sub Programs:

jens
Download Presentation

ICS 3UI – Introduction to Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICS 3UI – Introduction to Computer Science Sub Programs Rationale: allows division of large programming tasks aides debugging simplifies code so that humans can understand it allows code re-use Sub Programs: Sub-Programmes are small, named, autonomous, sections of code which are responsible for one or more related tasks. They are separate from the mainline program, which is the program that you have been writing all along. They are called by the mainline program, and control of the program execution is passed from the mainline to the sub program for its duration.

  2. ICS 3UI – Introduction to Computer Science Sub Programs There are two types of sub-programmes: Functions, which are responsible for calculating a single value Procedures, which are responsible for performing a single task, or set of related tasks Generally, this is thought of as “functions calculate value, procedures do something”. (In other programming languages, they may have different names. However, the distinction between these two types exists in all languages.)

  3. ICS 3UI – Introduction to Computer Science Sub Programs Procedures: Procedures are sub programs which “do something”. This may consist of setting up a GUI, obtaining user input, clearing a screen, performing a series of calculations (but not calculating a single value), working with a file, outputting data… A procedure must be defined before is can be used. Its definition includes its name, information being passed to it, and the code which should execute when it is called, as in: procedure procedureName do something end procedureName

  4. ICS 3UI – Introduction to Computer Science Sub Programs Procedure names should be verbs or actions. Example: procedure getData put “Enter name: “.. get sName put “Enter age: “.. get nAge put “Enter phone number: “.. get sPhoneNumber end getData

  5. ICS 3UI – Introduction to Computer Science Sub Programs put “This program will ask you some questions” put “<get ready>” delay (1000) getData put “Data obtained.” You can think of the code from the procedure being inserted into the mainline program at the place where the procedures is called.

  6. ICS 3UI – Introduction to Computer Science Sub Programs Program Form: A procedure (or function for that matter) must be defined before it can be used in a program. Therefore, the following program form must be followed for all programs: - header - function definitions - procedure definitions (it is assumed that the procedures will likely call some functions, and not the reverse) - global variables - mainline program The program can use something that has been declared above it.

  7. ICS 3UI – Introduction to Computer Science Sub Programs Note: A variable declared within a sub program is local to that sub program, and cannot be accessed outside of it. Variables declared outside of any sub program or construct are available anywhere in the program. In the above example, sName, nAge, and sPhoneNumber are all global variables, that have been declared somewhere else. It is also possible to pass specific values to a sub program, using parameters. This allows a sub program to do one thing over and over with different data sets.

  8. ICS 3UI – Introduction to Computer Science Sub Programs Passing Value: It is very helpful to have a procedure which can operate on many different pieces of data. To do this, we need to give it the piece of data that it needs each time it is called. This is accomplished with parameters. A parameter is a value that is passed to a sub program, which it uses to do its job. For example, the pred-defined procedure delay(). This procedure is written to delay a number of milliseconds, depending on the number you place in brackets. I.e.: delay(1000) %delays for 1000 milliseconds

  9. ICS 3UI – Introduction to Computer Science Sub Programs Depending on the value you put in brackets, the procedure accomplishes a different result. The procedure has a parameter, which is a variable that takes on the value passed to it by the mainline program. Here is another procedure: procedure showDouble (nNum:int) put nNum*2 end showDouble showDouble (5) showDouble(11) showDouble(11.3) % this causes an error In this example, nNum is a “variable” which takes on the value 5, then 11 (and then crashes).

  10. ICS 3UI – Introduction to Computer Science Sub Programs The procedure can show the double of any integer passed to it, since the parameter nNum can take on any integer value. The variable nNum is not declared anywhere except in the procedure definition line. Its value cannot be changed in showDouble, since it is not like a regular variable. The value that is being passed is called an argument. The variable that receives value is called a parameter. A sub program may have more than one parameter. The variables listed in brackets after the name of the sub program is called the parameter list. E.g. drawfillbox(x1,y1,x2,y2,colour)

  11. ICS 3UI – Introduction to Computer Science Sub Programs This procedure has 5 parameters, and you must send it 5 arguments. If you changed the order of your arguments, you will get the wrong box. If you leave out an argument, the program will not run. If you send the wrong type of data, the program will not run. Therefore, we say that arguments and parameters must agree in: - type - quantity - order If theydo not, the procedure will not function properly, if at all.

  12. ICS 3UI – Introduction to Computer Science Sub Programs Arguments are not like regular variables. However, their scope (like regular variables) is limited to the block of code they are declared in. For example: procedure doSomething(num:int) put num end doSomething num:=10 % not allowed This will cause an error.

  13. ICS 3UI – Introduction to Computer Science Sub Programs A function is a sub program which does some mathematical calculation. It calculates a single value. For example, the sqrt function in turing: put sqrt(100) Since the sqrt functions calculates a value (in this case, the number 10), it is used in conjunction with the put statement. Functions can be thought to “have value” (i.e., the value that it calculates), so the function must be used in the program as if it were an instance of the value it calculates. (This is different than procedures, which are simply used on lines all by themselves, as in: drawfillbox(1,1,10,10,blue) ) Functions are used in programs as values are used.

  14. ICS 3UI – Introduction to Computer Science Sub Programs The functions definition has the following form: function nameOfFunction(parameter list) : type result valueCalculatedByFunction %(of type declared above) end nameOfFunction The type returned in the result line must match the type of data declared in the function definition line. For example: function findSquareRoot(num : real) : real var dSquareRoot := num**0.5 result dSquareRoot end findSquareRoot

  15. ICS 3UI – Introduction to Computer Science Sub Programs % This statement below uses findSquareRoot as if it had the value indicated at the end of the result line. put findSquareRoot(88) % This is more or less the same thing as put dSquareRoot %error % Except that dSquareRoot is not a global variable so this will not work. Functions calculate and return a single value. Procedures do something or a related set of tasks.

  16. ICS 3UI – Introduction to Computer Science Sub Programs Functions are used so that a calculation does not need to be typed in over and over again. If you will be doing the same calculation repeatedly, it is often a good idea to use a function. Example: Create a function which calculates the volume of a box given its length, width, and height.

  17. ICS 3UI – Introduction to Computer Science Sub Programs Questions or Comments?

More Related