Matakuliah : D0524 / Algoritma dan Pemrograman Komputer Tahun : 2005 Versi : Pertemuan 08 Function
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Menerapkan penggunaan fungsi
Function Called in an IF Statement Function Called on an Output Line Outline Materi What is Function Features in a Function Uses of a Function Function Called in an IF Statement Function Called on an Output Line Function Used for Conversion
What is Function Often, a procedure is needed to do some specific task and return one single value. Examples: Validate an input value Convert some value, like: a sum of money from sterling to euros or a temperature from degrees Fahrenheit to degrees Celsius In such cases, a special type of procedure, called a function, can be used.
Is similar to a procedure in that it: Features of a Function Is similar to a procedure in that it: has a specific identifying name carries out a specific task may or may not take parameters is called or invoked by another procedure or program Is different in that: it has a data type associated with it it returns a single value to the calling program the function name itself contains the value being returned
Unlike procedures, functions can be used within other command, e.g. Uses of Function Unlike procedures, functions can be used within other command, e.g. As part of a condition in an IF statement If IsValid(rateFig) Then . . . As part of the output from a Me.Print statement Me.Print “Rata - Rata ” & HitungAvg(sum, number) As part of an assignment statement rupiah = HitungRupiah(uang)
Function Called in an IF Statement Private Function IsValid (reply as string) as boolean IsValid = False If reply = “Yes” Or reply = “No” Then IsValid = True End If End Function Calling the function: If IsValid(rateFig) Then Me.Print . . . . . Else . . . .
Function Called on an Output Line Private Function HitungAvg (s as Integer, n as Integer) as single CalcAvg = s/n End Function Calling the function: Me.Print “Rata – Ratanya : ” & HitungAvg(sum, number)
Function Used for Conversion Private Function HitungRupiah (UangDollar as Currency) as Currency HitungRupiah = UangDollar * 9700 End Function Calling the function: Dim UangDollar as Currency, UangRupiah as Currency UangDollar = InputBox(“Masukkan jumlah Dollar") UangRupiah = HitungRupiah(UangDollar)