跳轉到內容

SuperCard 程式設計/基礎/處理程式 & 函式

來自華夏公益教科書,一個開放的世界中的開放書籍

處理程式

[編輯 | 編輯原始碼]

SuperCard 中的處理程式是子例程。

它們以 "on" 關鍵字開頭,後面跟著處理程式名稱。它們以 "end" 關鍵字結束,後面也跟著處理程式名稱。

示例

on calculateInterest
   -- other lines of code would be here
end calculateInterest

處理程式可以接收一個或多個傳遞給它們的引數。這些引數應該包含在處理程式定義中,位於處理程式名稱之後,並用逗號分隔。

單個引數

[編輯 | 編輯原始碼]

這是一個名為 "calculateInterest" 的處理程式的示例,它接收一個名為 "baseAmt" 的引數,並將其乘以已定義的全域性變數 "gInterestRate",並將結果儲存在已定義的全域性變數 "gInterestAmt" 中。

示例

on calculateInterest baseAmt
   global gInterestRate
   global gInterestAmt

   put (baseAmt * gInterestRate) into gInterestAmt
end calculateInterest

為了在程式碼中呼叫此處理程式,您需要執行類似以下操作

使用固定的字面量

calculateInterest 5599

或者,像這樣,使用變數

calculateInterest preInterestAmt

多個引數

[編輯 | 編輯原始碼]

這是一個名為 "calculateInterest" 的處理程式的示例,它接收兩個引數 "baseAmt" 和 "interestRate",將它們相乘,然後將結果儲存在已定義的全域性變數 "gInterestAmt" 中。

示例

on calculateInterest baseAmt, interestRate
   global gInterestAmt

   put (baseAmt * interestRate) into gInterestAmt
end calculateInterest

為了在程式碼中呼叫此處理程式,您需要執行類似以下操作

使用固定的字面量

calculateInterest 5599,0.07

或者,像這樣,使用變數

calculateInterest preInterestAmt,anInterestRate

函式也像處理程式一樣是子例程,但是函式應該使用 "return" 關鍵字在終止函式的 "end [function_name]" 行之前返回值。SuperTalk 不會強制執行此操作。它允許您宣告不返回值的函式,就像它允許您在處理程式中返回值一樣。但是,最好不要從處理程式返回返回值,並在需要返回值時使用函式而不是處理程式。遵循此約定將幫助您編寫更易於理解、除錯和維護的程式碼。

呼叫函式的方式略微不同於呼叫處理程式。呼叫函式通常需要在函式名稱後面加上一對開閉括號,即使沒有傳遞引數。

這是一個不接收引數的函式示例

function piTimesTwo
   put pi * 2 into theResult

   return theResult
end piTimesTwo

為了呼叫此函式,您需要執行類似以下操作

put piTimesTwo() into doublePi

或者,您可以像這樣使用它而不儲存其結果

if someOtherVariable > piTimesTwo() then
   -- lines of code for a true result would go here
else
   -- actions for the false result would go here
end if

請注意,即使 "piTimesTwo" 函式不接收引數,在呼叫它時,它的名稱後面也會跟著一對空括號。

就像處理程式一樣,函式可以接收一個或多個引數。

單個引數

[編輯 | 編輯原始碼]

基於上面的示例,這裡有一個計算利息的函式,它只接收一個引數,將其乘以已定義的全域性變數,但不是將結果儲存在另一個全域性變數中,而是將其返回

function calculateInterest baseAmt
   global gInterestRate

   put (baseAmt * gInterestRate) into theResult

   return theResult
end calculateInterest

您可以像這樣使用字面量呼叫此函式

set tInterestAmt to calculateInterest(5995)

但是,更現實的是,您可能會將儲存在變數中的金額傳遞給它

set tInterestAmt to calculateInterest(somePreInterestAmt)

多個引數

[編輯 | 編輯原始碼]

最後,這是一個更現實的(儘管可能過於簡單)函式示例,它接收兩個引數,將它們相乘,然後返回結果。優點是,您不需要為利率定義全域性變數。之所以過於簡單,是因為您不需要定義一個函式來僅僅將兩個數字相乘並存儲結果,但是,我相信您可以想象建立比僅僅相乘兩個數字並返回結果更復雜得多的函式。

示例

function calculateInterest baseAmt,interestRate
   put baseAmt * interestRate into theResult

   return theResult
end calculateInterest

注意:實際上沒有必要將 (baseAmt * interestRate) 放入一個名為 "theResult" 的新變數中。前面的函式可以像這樣寫

function calculateInterest baseAmt,interestRate
   return (baseAmt * interestRate)
end calculateInterest

要使用儲存在兩個變數中的值呼叫函式,您需要執行類似以下操作

put calculateInterest(someAmt,someInterestRate) into someInterestAmt

處理程式和函式都允許您在 SuperTalk 指令碼中定義子例程。

處理程式和函式都可以接收

  • 無引數
  • 一個引數
  • 多個引數

處理程式以 "on" 關鍵字開頭,後面跟著處理程式名稱。

函式以 "function" 關鍵字開頭,後面跟著函式名稱。

處理程式和函式都以 "end" 關鍵字結束,後面跟著處理程式/函式名稱。

在呼叫接收一個或多個引數的處理程式時,引數將放置在處理程式名稱後面,不帶任何包圍括號。

在呼叫不接收引數的函式時,最好在函式名稱後面加上一對空括號。在呼叫接收一個或多個引數的函式時,引數必須用括號括起來。

您應該使用函式來建立使用 "return" 關鍵字後跟變數名或計算結果返回值的子例程。

您應該使用處理程式來建立不返回值的子例程。

華夏公益教科書