标题: Compare coding with function or not. [打印本页] 作者: weiwei 时间: 2010-10-15 18:00 标题: Compare coding with function or not. 本帖最后由 weiwei 于 2010-10-15 18:14 编辑
Because it takes 6 lines to draw each shape, we now have 12 lines. Drawing our desired pattern that uses 6 shapes will require 36 lines of code. Imagine if we wanted to draw 30 eyes—the code inside draw() would bloat to 180 lines.
在此提及到如果每個圖形都單圖控制,會造成程式的冗長。並且不好修改。
[attach]11695[/attach]
Because the shapes are identical, a function can be written for drawing them. The function introduced in the next example has two inputs that set the x-coordinate and y-coordinate. The lines of code inside the function render the elements for one shape.
function 的功能是拿來控制主體的框架,細部參數修改的部份就從draw()裡面自訂的function呼叫。
[attach]11696[/attach]
Now that the function is working, it can be used each time we want to draw that shape.
If we want to use the shape in another program, we can copy and paste the function. We no longer need to think about how the shape is being drawn or what each line of code inside the function does. We only need to remember how to control its position with the two parameters.
資料來源 processing handbook p-193作者: weiwei 时间: 2010-10-15 18:12
Multiple functions can have the same name, as long as they have different parameters.Creating different functions with the same name is called function overloading, and it’s what allows Processing to have more than one version of functions like fill(),image(), and text(), each with different parameters.
A program can also have two functions with the same number of parameters, but only if the data type for one of the parameters is different.
需要注意的是,function可以同樣命名處理不同的功能,但裡面的參數必須不一樣。
[attach]11697[/attach]
重複命名的特性就像processing 內建好的fill(),image(),text().一樣。這種功能稱做
Function overloading作者: claudemit 时间: 2010-10-15 19:12
可以处理value,需要定义function的返回值的种类,最后结束时return需要返回的值
void就是没有返回值作者: weiwei 时间: 2010-10-15 19:31
嗯嗯 多謝claudemit 的補充!! 如果要value 的就必須加上return 還有資料型態data type作者: wyj900817 时间: 2010-10-16 00:36
楼主能给个邮箱么(注册用户,没法发站内信的= =),目前和我的设计老师在做arduino相关,可以交流下么~~多谢了~~作者: lazyboy 时间: 2010-10-16 11:48
很精彩,
努力学习,看懂理解,作者: weiwei 时间: 2010-10-16 19:02 5#wyj900817
我已經發郵箱 給你了,你去站內收短消息巴。作者: zcm_2004 时间: 2010-12-24 15:43
nice thanks