Functions,Part 2 函式,Part 2

Functions,Part 2  函式,Part 2

Function Refresher 函式複習

Don’t Repeat Yourself! This is a very simple, yet fundamental principle in programming.
避免撰寫重複的程式碼!這很簡單但是在程式裡頭卻是最基礎的原則。

Whenever you feel the need to rewrite a block of code, remember that it can probably be written as a function instead. You've seen some of PHP's built-in functions, but you can also define your own!
當你覺得有必要重新將某區塊的程式碼再寫一次,記得也許將那段程式碼改寫成函式是比較好的作法。你已經看過了一些PHP內建的函式了,但你其實可以定義自己的函式。

By using one function instead of several blocks of the same code, you can reduce the amount of clutter in your document and keep your code neat and tidy.

透過使用一個函式而非重複寫相同程式碼好幾遍,你不但能夠有效減少程式碼長度,並得以保持其整齊與簡潔。


Instructions 操作指引


Let's do a little review. On line 8, use the strlen() function to echo the number of characters in your name.
檢視一下程式碼。在第8行,呼叫strlen()來echo輸出你的名字共有幾個字元。


Function Syntax 函式語法

The typical structure of a function is as follows:
典型的函式結構如下:


function name(parameters) {
  statement;
}

1.The keyword function indicates that the code following it will be a user-defined function.
1.關鍵字提示該程式碼後面將會是一個使用者定義的函式。

2.name indicates the name of a function, which is case insensitive. The name of a function can contain numbers, letters, underscores or dashes.
2.name這個區塊提示了函式的名稱,它是大小寫不敏感的,亦即大小寫都視為相同的,但不建議寫成大寫。名稱的組成可以包含了數字.英文字母.下底線以及連接線。

3.The arguments, or parameters, will be the optional input a function uses to perform calculations.
3.parameters區塊稱為參數列,這是可選輸入項用以提供資料給函式作為計算之用。

4.And of course, the statements themselves will be the code the function executes when it is called.
4.當然了,一定會有的statements執行程式碼區塊也就是當函式被呼叫時所要執行的內容。


Instructions 操作指引


Take a look at the helloWorld() function to the right. It takes no parameters, and when called it will simply print the string "Hello world!”.
看一下右邊的helloWorld() 函式。它沒有任何參數,當我們呼叫它的時候也僅僅是印出Hello world!字串。

Press Save & Submit Code to continue to the next exercise.
按下 Save & Submit Code來繼續到下一個練習。


Your First Function 你的第一個函式

Writing our name can be a dull task when we don't have fun functions to help out. In this exercise, we'll be writing our very first user-defined PHP function that will do just that for us!
寫我們的名字或許是個很沉悶的工作。在這個練習中,我們將要靠自己寫出第一個使用者定義的PHP函式!


Instructions 操作指引


On line 10, write a function called displayName, that takes no parameters and will echo a string that contains your name. Check out the Hint if you need help!
在第10行,寫一個名字為displayName的函式,它沒有任何的參數,並且將會echo印出一個包含你名字的字串。如果有需要的話可以確認一下提示!


Calling Your Function 呼叫你的函式

Defining our function makes it available for us to use, but we aren't using it until we call it.
定義我們的函式的目的是讓它為我們所用,但實際上如果我們不呼叫它是沒法用的。

To call a function, simply type the name of the function followed by any parameters:
要呼叫一個函式,只需要輸入函式的名稱並在後面加上參數列即可,假如沒有任何參數則只要在名稱後面補上一個不帶參數的()


functionName(parameters);

Instructions 操作指引


On the line after your function, try calling it. You should see your name display in the results panel!
在你定義函式區塊的後面,試著呼叫它。你應該會在結果面板看到你的名字顯示出來!


The Return Keyword 回傳關鍵字

Instead of printing something to the screen, what if you want to make it the value that the function outputs so it can be used elsewhere in your program? In PHP, the return keyword does just that. It returns to us a value that we can work with. The difference between this and echo or printis that it doesn't actually display the value.
除了在螢幕上面印出些什麼,假如你想要讓函式傳回一個值讓你能夠在其他地方使用該怎麼辦? 在PHP裡頭,return關鍵字就是為此而生的。它可以傳回一個值給我們作之後使用。它和 echo 或者是 print 的差別在於它並不會在螢幕上顯示任何東西。

Think of it like a calculator solving a mathematical problem that takes several steps to complete. The value from each step is computed, but we don't see the result until we get the final answer. In other words, each value is returned and the final answer is echoed on screen for us.
舉個例子來說,一個計算機用來解決一個數學問題可能需要很多個步驟才會完成。我們看不到每一個步驟完成所得到的結果,一直到最後的結果出來為止。換言之,除了最後一個步驟以外的結果都被return回傳回來,而最後步驟的結果則是echoed顯示在螢幕上面。


Instructions 操作指引


On line 8, write a function called returnName that returns your name.
在第8行,寫一個函式稱為returnName,它將會回傳你的名字。

Notice how nothing is displayed in the results panel. Even though it is not visible, calling our function will still give us back our string, we just haven't done anything with it yet.
注意到在結果面板將不會顯示任何東西。儘管沒有顯示任何內容,呼叫我們的函式將會傳回一個字串給我們,然而我們還沒作任何事情就是了。


Parameters and Arguments 參數們


Functions wouldn't be nearly as useful if they weren't able to take in some input. This is where parameters or arguments come in. These are the variables or inputs that a function uses to perform calculations.
無法接收資料的函式一般沒有太大的用處,這時候參數就派上用場了。比如說下面的函式需要取得數字作為計算之用,用來取得參數的變數就被稱為參數。


function squareValue($number) {
  echo $number * $number;
}

$n = 6;
squareValue($n); // echos 36

The function squareValue, above, takes one parameter, which it multiplies by itself and then echos the result. The names of the parameters themselves are used internally within the function, so they should be named something helpful.
上面的squareValue函式取得一個參數,那讓它能夠將之取平方之後將結果印出來。而帶名字的參數本身就是一個只存在於函式裡頭的變數,這就是它們會需要一個名字的原因。

You can also use multiple parameters as long as they are separated by commas.
你也能夠使用更多的參數只要將它們用逗號分隔開來即可。


Instructions 操作指引


01.Write a function called greetings.
01. 寫一個函式取名為greetings
02.It should take $name as a parameter.
02. 它應該擁有一個名為$name的參數
03.The function should echo a string equal to "Greetings, " . $name . "!”.
03. 該函式需要印出一個字串,內容為”Greetings, “ . $name . “!”.
04.Then call greetings() with your name to test it out!
04.接著呼叫greetings()並傳入你的名字來試試看是否成功!


Practice Makes Perfect 練習讓你更純熟

Hopefully throughout these exercises you have learned the basics of defining and calling your own functions using PHP. To summarize this section we are going to have to take all of what we have learned and apply it once more!
希望這些練習讓你得以掌握定義和呼叫PHP函式的基礎。作為這個章節的總結我們將要把剛才所學全部用出來,並且靠自己再做一次。


Instructions 操作指引


01.Write a function called aboutMe.
01. 定義一個名為 aboutMe的函式
02.It takes two parameters $name and $age.
02. 它將會有兩個參數,分別是$name$age.
03.It should echo out "Hello! My name is$name, and I am $age years old.”.
03. 它應該印出"Hello! My name is$name, and I am $age years old.”.
04.Call your function and check out the results panel to see if it works!
04. 呼叫你的函式並確認結果是否與你所預期的相同!

分享這篇文章:

發佈自 林品爵

林品爵
一個堅信程式教育能夠改變下一代未來的狂熱者,青少年揚帆計畫的推動者。十年以上全端程式開發,上百場程式教學經驗,累積學生數達百人以上。 成大學士畢業,使用過J2EE.PHP.MySQL.CoronaSDK.Android.Objective-C.Swift.Python等技術知識,歷任友訊科技總部Java工程師首席.黑快馬股份有限公司研發經理.諾亞數碼娛樂執行長等職位,現任哥布林程式教育學苑創辦人,所開發過專案涵括產品管理系統.POS系統.拍賣平台.擴增實境.問券.社區App等等。 程式啟蒙班講師 . Laravel網頁程式班講師 . App程式班講師

關聯文章:

留下留言