Pemrograman komputer 7 (prosedur)

786 views

Published on

unj fmipa-fisika

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
786
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Pemrograman komputer 7 (prosedur)

  1. 1. Pemrograman Komputer Pertemuan 07 Procedures Bambang Heru Iswanto, Dr.rer.nat M.Si <ul><li>Jurusan Fisika </li></ul><ul><li>Fakultas Matematika dan Ilmu Pengetahuan Alam </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  2. 2. Learning Outcomes <ul><li>Pada akhir pertemuan ini, diharapkan mahasiswa </li></ul><ul><li>akan mampu : </li></ul><ul><li>Menerapkan penggunaan prosedur </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  3. 3. Outline Materi <ul><li>Procedures </li></ul><ul><li>Needed for Procedures </li></ul><ul><li>Parameter Passing </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  4. 4. Procedures <ul><li>In program design, we break the overall task into sub tasks. These subtasks are implemented using procedures. Each procedure should carry out one sub task. </li></ul><ul><li>A procedure is simply a way of performing  code 'out of line'. When a program encounters a call to a procedure, it suspends its current path through the code statements and passes control to a separate chuck of code. When this separate chuck has been executed, control returns to the statement immediately following the call: </li></ul><ul><li>Private Sub cmdButton_Click() </li></ul><ul><li>… </li></ul><ul><li>Call aProcedure </li></ul><ul><li>… </li></ul><ul><li>End Sub </li></ul>Private Sub aProcedure … End Sub 01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  5. 5. Procedures <ul><li>All procedures consist of a header that indicates the procedure name and any information that it requires </li></ul><ul><li>Body that performs the task </li></ul><ul><li>If a procedure is to be used by a number of different forms it should be declared as Public within the code module otherwise it should be declared as Private in the general declaration section of the form. </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  6. 6. Needed for Procedures <ul><li>A simplification of the program structure </li></ul><ul><li>Reduction in the amount of code that is repeated </li></ul><ul><li>Ability to focus on what needs to be done rather than how it is to be achieved </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  7. 7. Parameter Passing <ul><li>Procedures are not very useful though unless they can communicate data </li></ul><ul><li>Data passed between procedures are called parameters </li></ul><ul><li>Parameters can be sent to the procedure for use within it, e.g. calculations </li></ul><ul><li>Results from the procedure can be sent back by the procedure to the calling program </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  8. 8. Parameter Passing - Example Main keyInput num1 keyInput num2 IF num1 < num2 THEN Call Swap(num1, num2) ENDIF conOutput “Largest”, num1 Swap (num1, num2) temp = num1 num1 = num2 num2 = temp The variables num1 and num2 are passed to the procedure Swap for exchanging. After the procedure, the largest number is in num1 . 01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  9. 9. Parameter Passing – VB Solution <ul><li>Private Sub Form_Click() </li></ul><ul><li>num1 = InputBox(&quot;enter number 1&quot;) </li></ul><ul><li>num2 = InputBox(&quot;Enter number 2&quot;) </li></ul><ul><li>If num1 < num2 Then </li></ul><ul><li>Call Swap(num1, num2) ‘call to the procedure </li></ul><ul><li>End If </li></ul><ul><li>MsgBox &quot;Largest &quot; & num1 </li></ul><ul><li>End Sub </li></ul><ul><li>Private Sub Swap(num1 As Integer, num2 As Integer) </li></ul><ul><li>Dim temp As Integer ‘temp is a var. local to Swap </li></ul><ul><li>temp = num1 </li></ul><ul><li>num1 = num2 </li></ul><ul><li>num2 = temp </li></ul><ul><li>End Sub </li></ul>Parameter list for procedure 01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  10. 10. Parameter Passing - Rules <ul><li>Values are copied into the parameters The order of parameters declared in the procedure MUST match the order in which they were called </li></ul><ul><li>The data types must match. E.g. 1 is OK, e.g. 2 is not and will crash program! </li></ul><ul><li>E.g. 1 Call Swap(num1, num2) </li></ul><ul><li>Private Sub Swap(num1 As Integer, num2 As Integer) </li></ul><ul><li>E.g. 2 Call AnyProc(myName, myAge) </li></ul><ul><li>Private Sub AnyProc(myAgeAs Integer, myName As String) </li></ul>01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |
  11. 11. TERIMA KASIH 01/02/11 © 2010 Universitas Negeri Jakarta | www.unj.ac.id |

×