1 / 6

Session Objectives #U2 S8

Session Objectives #U2 S8. Key Words. Function. Subroutine. Parameter. Procedure. Recursion. KEYWORDS EXPLAINED. Sub routines are sections of code which perform a specific task which can be separated from the main program. They are called either procedures or functions .

afya
Download Presentation

Session Objectives #U2 S8

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. Session Objectives#U2 S8

  2. Key Words Function Subroutine Parameter Procedure Recursion

  3. KEYWORDS EXPLAINED Sub routines are sections of code which perform a specific task which can be separated from the main program. They are called either procedures or functions. A function performs a specific task and returns a single value i.e input 1 add input 2, output answer A procedure performs a task or operation but does not return a value i.e print file or open file Parameters – when a function or procedure is defined information about the data item(s) being supplied are given. This is known as the parameters are usually expressed in (brackets) after the identifier.

  4. PARAMETERS Parameters are needed to tell the computer what data types are being used for each variable in the subroutine, and in the case of functions, the data type of the ‘return value’ (the single value returned after the instructions have been carried out). What do these tell us? PROCEDURE Print Details (Name: String, Copies: Integer) FUNCTION Interest(AMT:Currency, Rate:Real, Yrs:Integer):Currency • The Procedure name is Print Details • The parameter Name is of data type String • The parameter Copies is of Integer • The function name is interest • Amt of data type currency • Rate of data type Real • Yrs of data type Integer • The return value of the function is of data type Currency

  5. FUNCTIONS Functions need to be defined before they can be called (or used) in the main body of the program. In Python deffunction(): instructions… is the method for defining the function and it can be called again later in the program with a simple function() TASK: Look at the addcalc.py example and then apply to your multicalc.py TASK 2: Compare your original program with the functional version and discuss which you think is the more efficient. Prepare to answer the questions of when or why should you use functions in your program design.

  6. RECURSION This is when a subroutine (procedure or function) calls itself. The subroutine executes instructions as normal until it calls itself, the subroutine then pauses and it goes through the process again until the conditions set out in the recursive subroutine are met – this is known as the stopping condition. So in a recursive subroutine it is also iterative in nature in the sense that it repeats itself until the stopping condition is met. See the example on p83-84 for further explanation and create a table of strengths/weakness of iteration vs recursion (p.85) Now answer the exam style questions on p85-86 (Complete for Hwk)

More Related