Tutorial Python untuk bukan pengatur cara/Contoh Fungsi Lanjutan: Perbezaan antara semakan

Kandungan dihapus Kandungan ditambah
Algazel (bincang | sumb.)
kembang
Algazel (bincang | sumb.)
bab selesai, kod BM OK
Baris 85:
: Kita perlu melaksanakan kod sekali lagi bagi mendapatkan jawapannya. Kali ini nilai <code>a</code> adalah 3 dan nilai <code>b</code> adalah 0.
----
; Apa yang berlaku seterusnya?
; So what happens next?
: TheBaris firstpertama lineyang indilaksanakan thefungsi function to run isadalah <code>if b == 0:</code>. <code> b</code> has themempunyai valuenilai 0, sojadi thebaris nextyang linedilaksanakan toberikutan runini isadalah <code>return 0</code>
----
; AndDan whatpa doesyang thedilakukan linebaris <code>return 0</code> do?
: Baris ini memulangkan nilai 0 daripada fungsi.
: This line returns the value 0 out of the function.
----
; SoJadi?
: SoJadi nowsekarang wekita knowtahu thatyang <code>mult(3, 0)</code> hasmemiliki the valuenilai 0. NowSekarang wekita knowtahu whatapa theyang linedilakukan <code>rest = mult(a, b - 1)</code> didselepas sinceterlaksananya we have run the functionfungsi <code>mult</code> withdengan the parametersparameter-parameter 3 anddan 0. WeKita havesudah finishedselesai runningmelaksanakan <code>mult(3, 0)</code> anddan arekini nowpulang backkepada topelaksanaan runningsemula <code> mult(3, 1)</code>. The variablePemboleh ubah <code>rest</code> gets assignedmenerima theumpukan valuenilai 0.
----
; Baris mana yang dilaksanakn selepas ini?
; What line is run next?
: The lineBaris <code>value = a + restbaki</code> is run nextdilaksanakan. In thisDalam runpelaksanaan of the functionini, <code>a = 3</code> anddan <code>restbaki = 0</code>, sojadi nowsekarang <code>valuenilai = 3</code>.
----
; Seterusnya?
; What happens next?
: The lineBaris <code>return valuenilai</code> is rundilaksanakan. ThisIni returnsmemulangkan 3 fromdaripada the functionfungs. This alsoIni exitsjuga fromkeluar thedaripada run of the functionfungsi <code> mult(3, 1)</code>. AfterSelepas <code>return</code> is calleddipanggil, wekita gopulang backkembali tokepada runningpelaksanaan <code> mult(3, 2)</code>.
----
; WhereDi weremana wekita indalam <code> mult(3, 2)</code>?
: WeKita hadada thepemboleh ubah variables <code>a = 3</code> anddan <code>b = 2</code> and weredan examiningsedang themeneliti linebaris <code>restbaki = mult(a, b - 1)</code>.
----
; SoApa whatyang happensberlaku nowkini??
: ThePemb variableoleh ubah <code>restbaki</code> getdiumpukkan nilai 3 assigned to it. TheBari next lineberikutnya <code>value = a + restbaki</code> setsmenetapkan <code>valuenilai</code> tokepada <code>3 + 3</code> oratau 6.
----
; Jadi selepas ini apa yang berlaku?
; So now what happens?
: TheBaris nextberikutnya line runsdilaksanakan, thisini returnsmemulangkan 6 fromdaripada the functionfungsi. WeKita arekini nowpulang backkepada to running the linepelaksanaan <code>print "3 * 2 = ", mult(3, 2)</code> whichyang can now printsekarang outboleh themencetak 6.
----
; Apa yang berlaku secara keseluruhan?
; What is happening overall?
: BasicallySecara weasasnya usedkita twomengguna factsdua tofakta calculateuntuk themengira multiplegandaan ofdua thenombor. two numbers.Yang pertama Theadalah, firstapa isjua thatnombor anyyang numberdidarab timesdengan 0 isadalah 0 (<code>x * 0 = 0</code>). TheYang secondkedua isadalah thatsesuatu anombor numberdidarab timesdengan anothernombor numberlain issama equaldengan tonombor thepertama firstditambah numbernombor pluspertama thekali firstsekali numberkurang timesdari onenombor lesskedua than the second number (<code>x * y = x + x * (y - 1)</code>). SoJadi whatapa happensyang isberlaku adalah <code>3 * 2</code> ismula-mual firstdiubah converted intomenjadi <code> 3 + 3 * 1</code>. ThenKemudian <code>3 * 1</code> isdiubah converted intokepada <code>3 + 3 * 0</code>. ThenKemudian wekita knowtahu thatbahawa anyapa numberjua timesnombor didarab dengan 0 isadalah 0, jadi so <code>3 * 0</code> isadalah 0. ThenKemudian wekita candapat calculatekira thatbahawa <code>3 + 3 * 0</code> isadalah <code>3 + 0</code> whichyang issama dengan <code>3</code>. NowSekarang kita wetahu knowapa whatmakna <code>3 * 1</code>, isjadi sokita wedapat canmembuat calculateperkiraan bahawa that <code>3 + 3 * 1</code> isadalah <code>3 + 3</code> whichyang sama dengan is <code>6</code>.
----
Ini caranya kod bekerja:
This is how the whole thing works:
 
3 * 2
Baris 121:
6
 
Lihat proses ini cara terbalik jika anda masih menemui kesulitan dengan contoh ini. Apakah langkah terakhir yang berlaku? Kita dapat kira bahawa keputusan <code>mult(3, 0)</code> adalah
Should you still have problems with this example, look at the process backwards. What is the last
step<code>0</code>. thatOleh happens?sebab We<code>b</code> canadalah easily make out that the result<code>0</code>, offungsi <code>mult(3, 0)</code> is
<code>0</code>.akan Since <code>b</code> ismemulangkan <code>0</code>, thedan function <code>mult(3, 0)</code>berhenti.
will return <code>0</code> and stop.
 
SoApa whatpula doesyang thedilakukan previouslangkah step dosebelumnya? <code>mult(3, 1)</code> does nottidak returnmemulangkan <code>0</code>
becausekerana <code>b</code> is notbukan <code>0</code>. So the nextJusteru linesbaris-baris areberikutnya executeddilaksanakan:
<code>restbaki = mult (a, b - 1)</code>, whichyang issama dengan <code>rest = mult (3, 0)</code>,
whichyang isjuga sama dengan <code>0</code> asseperti weyang justkita workedkira outtadi. SoJadi nowsekarang thepemboleh variableubah <code>restbaki</code> is setditetapkan tosebagai <code>0</code>.
 
TheBaris nextberikutnya linemenambah adds the value ofnilai <code>rest</code> tokepada <code>a</code>, anddan sinceoleh sebab <code>a</code> isadalah <code>3</code> anddan <code>rest</code> adalah <code>0</code>, keputusannya adalah <code>3</code>.
is <code>0</code>, the result is <code>3</code>.
 
NowKita wetahu knowbahawa that the functionfungsi <code>mult(3, 1)</code> returnsmemulangkan <code>3</code>. ButTetapi kita mahu tahu keputusan <code>mult(3,2)</code>. Justeru, kita perlu melompat kembali kepada permulaan program dan welaksanakannya wantsekali tolagi:
<code>mult(3, 2)</code> menetapkan <code>baki</code> kepada keputusan <code>mult(3, 1)</code>. Kita tahu daripada pusingan terakhir bahawa keputusan ini adalah <code>3</code>. Kemudian <code>nilai</code> menghitung <code>a + baki</code>, iaitu <code>3 + 3</code>. Kemudian keputusan 3 * 2 dicetak sebagai 6.
know the result of <code>mult(3,2)</code>. Therefore, we need to jump back to the
start of the program and execute it one more round:
<code>mult(3, 2)</code> sets <code>rest</code> to the result of <code>mult(3, 1)</code>. We know
from the last round that this result is <code>3</code>. Then <code>value</code> calculates as <code>a + rest</code>,
i. e. <code>3 + 3</code>. Then the result of 3 * 2 is printed as 6.
 
Pengajaran daripada contoh ini adalah fungsi <code>mult(a, b)</code> memulakan dirinya sendiri dalam dirinya. Fungsi ini melakukan ini sehingga <code>b</code> mencapai <code>0</code> dan kemudiannya menghitung keputusan seperti dijelaskan di atas.
The point of this example is that the function <code>mult(a, b)</code> starts itself inside
itself. It does this until <code>b</code> reaches <code>0</code> and then calculates the result as explained above.
 
==== RecursionRekursi ====
 
Programming constructs of this kind are called ''recursive'' and probably the most intuitive definition of ''recursion'' is:
Binaan pengaturcaraan jenis ini dipanggil ''rekursi'' dan mungkin definisi intuitif ''rekursi'' adalah:
----
; Rekursi
; Recursion
: IfJika youanda stillmasih don't gettidak itfaham, seelihat ''recursionrekursi''.
----
 
Bahagian-bahagian terakhir ini baru ditulis. Jika anda mempunyai ulasan, terjumpa kesilapan, ataupun berfikiran bahawa saya perlu memberi lebih banyak penjalasan atau penjelasan yang lebih terang, sila kirim e-mel. Saya pernah membuat perkara yang senang menjadi rumit dan sukar/tidak dapat difahami.
These last two sections were recently written. If you have any
Sekiranya baki tutorial ini jelas tetapi bahagian ini tidak, ia mungkin disebabkan kesilapan saya dan saya ingin tahu.
comments, found any errors or think I need more/clearer explanations
Terima kasih.
please email. I have been known in the past to make simple things
incomprehensible. If the rest of the tutorial has made sense, but this
section didn't, it is probably my fault and I would like to know.
Thanks.
 
=== Contoh ===