ICT
the blog for ICT
ken013194
暱稱: ホロ
性別: 男
國家: 香港
地區: 葵青區
« July 2026 »
SMTWTFS
1234
567891011
12131415161718
19202122232425
262728293031
最新文章
Text File
Arrays
String Procedures
String Functions
String Operation
文章分類
全部 (23)
Programming Language (23)
訪客留言
最近三個月尚無任何留言
每月文章
日誌訂閱
尚未訂閱任何日誌
好友名單
尚無任何好友
網站連結
尚無任何連結
最近訪客
最近沒有訪客
日誌統計
文章總數: 23
留言總數: 0
今日人氣: 0
累積人氣: 83
站內搜尋
RSS 訂閱
RSS Feed
2009 年 9 月 11 日  星期五   晴天


Subprograms(Subroutines) 分類: Programming Language

High-level Programming Language (Pascal)

Subprograms (also called Subroutines)

  • is a group of statements designed to carry out a specific task
  • there are two types of subprograms : procedures and functions
  • to perform the same set of instructions on different sets of data or at different locations in the program
  • since function is not covered in the syllabus, it will not be discussed
  • the main difference between the two is that

    a function returns a value and can be used in expressions, like this :
      x := sqrt (A);

    while a procedure is called to perform one or more tasks :

      writeln ('This is a test.');

  • declaration of subprograms should be placed between the variable declaration and the program body

    Declaration of Pascal Procedure

    consist of three parts : procedure heading, local declaration part and procedure body (action part) which appear in a fixed order

    Major parts
    Example
    Procedure heading procedure ProcedureName (Formal_parameter_list : Data_type) ;
    Local Declaration part const
    type {not covered in HKCEE syllabus}
    var
    procedure
    function {not covered in HKCEE syllabus}
    Procedure body begin
     statement_1 ;
     statement_2 ;
       ...
       ...
     statement_n
    end ;
  • differences between procedure and main program :
    • procedure starts with a procedure header instead of a program header, and they end with a semicolon instead of a period
    • procedure can have its own constants, and variables, and even its own subprograms, all these items can only be used with the procedure in which they are declared
    • variables declared in the declaration part of the main program are called Global Variables and can be accessed anywhere in the program
    • variables declared in the declaration part of the procedure are called Local Variables and can only be accessed within the procedure

  • Calling a Procedure
    • procedure can be called by a procedure call statement in a main program or a subprogram
    • whenever a procedure is called, the control is transferred from the calling program to the procedure reference and after the execution of procedure reference, the control is transferred back to the calling program or a subprogram and the next program statement will be executed
    • the calling of procedures

    the structure of procedure call statement
    Procedure_Name (Actual_parameter_list); or Procedure_Name ;

  • Parameters and Procedure Calls
    • values are passed into and out of procedures via the procedure’s parameters (and via non-local variables)
    • the parameters which are declared in the procedure heading are called the formal parameters, the values and variables which match the formal parameters in the procedure call are the actual parameters
    • the number of actual parameters must be the same as the number of formal parameters
    • the order and type of actual parameters must match exactly with the formal parameters
    • the value of each actual parameter is assigned to the corresponding formal parameter when the procedure is called, a correspondence between actual parameters and formal parameters is shown below :

  • Formal Parameters
    • can be classified as value parameter and variable parameter, for example


     procedure AreaOf Rectangle (Width, Length : real ; var Area : real);

    • formal parameters Width and Length are value parameters
      • which receive information passed into the procedure, the actual parameter corresponding to the value parameter can be a variable, constant or Pascal expression
      • at the time of procedure call, the values of the actual parameters are passed into the value parameters, there is no further connection between the formal and the actual parameters; therefore, any changes to the formal parameters will not affect the corresponding actual parameters
      • the calling is known as calling by value
    • formal parameter Area is a variable parameter
      • which will return the result of the procedure to the calling program, the actual parameter corresponding to the variable parameter must be a variable
      • the reserved word var must be placed before the variable parameter
      • the variable parameter refers to the same memory location as the actual parameter, thus any changes in the variable parameter in the called procedure represents a change in the actual parameter being passed
      • the calling is known as calling by reference

 






訪客留言 (返回 ken013194 的日誌)

訪客名稱:
電郵地址: (不會公開)
驗證碼:  按此更新驗證碼 (如看不清楚驗證碼請點擊圖片刷新)
俏俏話: (必需 登入 後才能使用此功能)
[ 開啟多功能編輯器 ]