1 / 21

Lab 10: Subroutines

Lab 10: Subroutines. Lab 10: Subroutines. Objectives: Program Sub-routines Pass parameters into a subroutine Modify an existing program to use subroutines. What are Subroutines?

jamij
Télécharger la présentation

Lab 10: Subroutines

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. Lab 10: Subroutines

  2. Lab 10: Subroutines Objectives: • Program Sub-routines • Pass parameters into a subroutine • Modify an existing program to use subroutines

  3. What are Subroutines? A subroutine (also called procedure, method, function, or routine) is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code. As the name "subprogram" suggests, a subroutine behaves in much the same way as a computer program that is used as one step in a larger program or another subprogram. A subroutine is often coded so that it can be started ("called") several times and/or from several places during a single execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the "call" once the subroutine's task is done.

  4. What are Subroutines? (Continued) Subroutines are a powerful programming tool, and the syntax of many programming languages includes support for writing and using them. Judicious use of subroutines (for example, through the structured programming approach) will often substantially reduce the cost of developing and maintaining a large program, while increasing its quality and reliability. Subroutines can simplify large programs by dividing common parts into separate small programs called as a subroutine.

  5. CODE CODE CODE CODE CALL CODE CODE RETURN CODE Subroutine Flow Chart Example:

  6. Subroutines CALL The CALL and CALLS instructions are used in V+ to implement subroutine calls. The CALL instruction causes program execution to be suspended and execution of a new program to begin. When the new program has completed execution, execution of the original program resumes at the instruction after the CALL instruction. The details of subroutine creation, execution, and parameter passing are covered in the next lab.

  7. The advantages of breaking a program into subroutines include: • decomposition of a complex programming task into simpler steps: this is one of the two main tools of structured programming, along with data structures. • reducing the duplication of code within a program, • enabling the reuse of code across multiple programs, • dividing a large programming task among various programmers, or various stages of a project, • hiding implementation details from users of the subroutine.

  8. High-level programming languages usually include specific constructs for • delimiting the part of the program (body) that comprises the subroutine, • assigning a name to the subroutine, • specifying the names and/or types of its parameters and/or return values, • providing a private naming scope for its temporary variables, • identifying variables outside the subroutine that are accessible within it, • calling the subroutine, • providing values to its parameters, • specifying the return values from within its body, • returning to the calling program, • disposing of the values returned by a call, • handling any exceptional conditions encountered during the call, • packaging subroutines into a module, library, object, class, etc.

  9. Passing Parameters: Subroutine may be written so that it expects to obtain one or more data values from the calling program (its parameters or arguments). It may also return a computed value to its caller (its return value), or provide various result values or out(put) parameters. Indeed, a common use of subroutines is to implement mathematical functions, in which the purpose of the subroutine is purely to compute one or more results whose values are entirely determined by the parameters passed to the subroutine. Advantage: As in the lab you are about to, one subroutine can be called to place different size blocks at different locations instead oh having a subroutine for each size block.

  10. Passing Parameters into a Subroutine: In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine.[1]. These pieces of data are called arguments. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call can be assigned to the corresponding parameters. In the most common case, call-by-value, a parameter acts within the subroutine as a local (isolated) copy of the argument, but in other cases, e.g. call-by-reference, the argument supplied by the caller can be affected by actions within the called subroutine (as discussed in evaluation strategy). The semantics for how parameters can be declared and how the arguments get passed to the parameters of subroutines are defined by the language, but the details of how this is represented in any particular computer system depends on the calling conventions of that system.

  11. Parameters and arguments These two terms are sometimes loosely used interchangeably; in particular, "argument" is sometimes used in place of "parameter". Nevertheless, there is a difference. Properly, parameters appear in procedure definitions; arguments appear in procedure calls. A parameter is an intrinsic property of the procedure, included in its definition. For example, in many languages, a minimal procedure to add two supplied integers together and calculate the sum total would need two parameters, one for each expected integer. In general, a procedure may be defined with any number of parameters, or no parameters at all. If a procedure has parameters, the part of its definition that specifies the parameters is called its parameter list. By contrast, the arguments are the values actually supplied to the procedure when it is called. Unlike the parameters, which form an unchanging part of the procedure's definition, the arguments can, and often do, vary from call to call. Each time a procedure is called, the part of the procedure call that specifies the arguments is called the argument list. Although parameters are also commonly referred to as arguments, arguments are more properly thought of as the actual values or references assigned to the parameter variables when the subroutine is called at runtime.

  12. The figure below shows the mapping of an argument list in a CALL statement to the argument list in a subroutine. The arrows indicate that each item in the list must match in position and data type but not necessarily in name. (The CALL statement argument list can include values and expressions as well as variable names.) instruction in main program: CALL a_routine(loc_var_a, real_var_a, 43.654, $string_var_a) subroutine program header .PROGRAM a_routine(any_loc, any_real_x, any_real_y, $any_string)

  13. Subroutine Instructions: The CALL instruction is used in V+ to implement subroutine calls. The CALL instruction causes program execution to be suspended and execution of a new program to begin. When the new program has completed execution, execution of the original program resumes at the instruction after the CALL instruction. CALL program (arg_list) program is the name of the program to be called. The program name must be specified exactly, and the program being CALLed must be resident in system memory. arg_list is the list of arguments being passed to the subroutine. These arguments can be passed either by value or by reference and must agree with the arguments expected by the program being called. The code: 48 . 49 CALL check_data(locx, locy, length) 50 . suspends execution of the calling program, passes the arguments locx, locy, and length to program check_data, executes check_data, and (after check_data has completed execution) resumes execution of the calling program at step 50.

  14. CALL Examples CALL pallet(count) Branches to the program named pallet, passing to it a pointer to the variable count. When a RETURN instruction is executed, control returns to the program containing the CALL instruction and count will contain the current value of the corresponding subroutine argument. CALL cycle(1, , n+3) Branches to the program named cycle. The value 1 is passed to the first parameter of cycle, its second parameter is undefined, and its third parameter receives the value of the expression n+3. (If cycle has more than three parameters, the remaining parameters are all undefined.) Since none of the arguments in the CALL are variables, no data will be returned by the program cycle.

  15. RETURN Terminate execution of the current subroutine, and resume execution of the suspended program at its next step. A program may have been suspended by issuing a CALL. A RETURN instruction in a main program has the same effect as a STOP instruction. A RETURN instruction is assumed if program execution reaches the last step of a subroutine. However, it is not good programming style to use this feature—an explicit RETURN instruction should be included as the last line of each subroutine.

  16. Program Example: This program calls a subroutine for picking up a part from the parts feeder position. A second subroutine is called for placing a part on the pallet. Parameters are passed for each position on the pallet.

  17. Parameters passed to subroutine Place3 And Type 3

  18. Parameters passed to subroutine Placex And Type x

  19. P2 P3 P4 Conveyor Pallet Photo Sensor & Reflector P1 Project figure 9-1 Parts Track Parts Pallet Push blocks up with a stick Lab Assignment: Modify lab 9 so that a subroutine is called each time a new block is picked up from the part feeder since that location will be the same for all three blocks. See the list below for program requirements.

  20. P2 P3 P4 Conveyor Pallet Photo Sensor & Reflector P1 Project figure 9-1 Parts Track Parts Pallet Push blocks up with a stick • Prompt for number of blocks • Use a counter for testing the number of blocks to be placed. • Print (Type) statements for each block placed • Prompt for speeds • Call a subroutine for GETBLOCK ( ) to pick up the block P1. • Call a Subroutine for PLACEBLOCK( ) Pass parameters into the subroutine for position P2, P3, or P4

  21. Lab 10: Subroutines The End

More Related