參數(shù)的多少與你的問(wèn)題的復(fù)雜度和解決問(wèn)題的方式都有一定的關(guān)聯(lián)。如果一個(gè)函數(shù)處理的問(wèn)題所需要的參數(shù)就是那么多的話,就是合適的,不必修改。
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到無(wú)極網(wǎng)站設(shè)計(jì)與無(wú)極網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、虛擬主機(jī)、企業(yè)郵箱。業(yè)務(wù)覆蓋無(wú)極地區(qū)。
fun函數(shù)聲明錯(cuò)誤,正確是?char fun(char a[20], char b);
int b; 變量 b重復(fù)定義,此句去掉。
fun(a[20], e);? 函數(shù)參數(shù)傳遞錯(cuò)誤,正確的是?fun(a, e);
正確代碼如下:
#include?stdio.h
#includestring.h
int?main()
{
char?fun(char?a[20],?char?b); //?函數(shù)聲明錯(cuò)誤
char?a[20],?e,?b[200];
//?int?b; //?b重復(fù)定義了,此句去掉
gets(a);
scanf("%c",?e);
fun(a,?e); //?a?參數(shù)傳遞錯(cuò)誤,正確?fun(a,?e)
}
char?fun(char?a[20],?char?b)
{
int?i;
for?(i?=?0;?i20;?i++)
if?(a[i]?==?b)
a[i]?=?a[i?+?1];
return?a[20];
}
一個(gè)函數(shù)的參數(shù)的數(shù)目沒有明確的限制,但是參數(shù)過(guò)多(例如超過(guò)8個(gè))顯然是一種不可取的編程風(fēng)格。參數(shù)的數(shù)目直接影響調(diào)用函數(shù)的速度,參數(shù)越多,調(diào)用函數(shù)就越慢。另一方面,參數(shù)的數(shù)目少,程序就顯得精練、簡(jiǎn)潔,這有助于檢查和發(fā)現(xiàn)程序中的錯(cuò)誤。因此,通常應(yīng)該盡可能減少參數(shù)的數(shù)目,如果一個(gè)函數(shù)的參數(shù)超過(guò)4個(gè),你就應(yīng)該考慮一下函數(shù)是否編寫得當(dāng)。 如果一個(gè)函數(shù)不得不使用很多參數(shù),你可以定義一個(gè)結(jié)構(gòu)來(lái)容納這些參數(shù),這是一種非常好的解決方法。在下例中,函數(shù)print_report()需要使用10個(gè)參數(shù),然而在它的說(shuō)明中并沒有列出這些參數(shù),而是通過(guò)一個(gè)RPT_PARMS結(jié)構(gòu)得到這些參數(shù)。 # include atdio. h typedef struct ( int orientation ; char rpt_name[25]; char rpt_path[40]; int destination; char output_file[25]; int starting_page; int ending_page; char db_name[25]; char db_path[40]; int draft_quality; )RPT_PARMS; void main (void); int print_report (RPT_PARMS* ); void main (void) { RPT_PARMS rpt_parm; /*define the report parameter structure variable * / /* set up the report parameter structure variable to pass to the print_report 0 function */ rpt_parm. orientation = ORIENT_LANDSCAPE; rpt_parm.rpt_name = "QSALES.RPT"; rpt_parm. rpt_path = "Ci\REPORTS" rpt_parm. destination == DEST_FILE; rpt_parm. output_file = "QSALES. TXT" ; rpt_parm. starting_page = 1; rpt_pann. ending_page = RPT_END; rpt_pann.db_name = "SALES. DB"; rpt_parm.db_path = "Ci\DATA"; rpt_pann. draft_quality = TRUE; /*call the print_report 0 function; paaaing it a pointer to the parameteM inatead of paMing it a long liat of 10 aeparate parameteM. * / ret_code = print_report(cu*pt_parm); } int print_report(RPT_PARMS*p) { int rc; /*acccM the report parametcra paaaed to the print_report() function */ oricnt_printcr(p-orientation); Kt_printer_quality((p-draft_quality == TRUE) ? DRAFT ; NORMAL); return rc; } 上例唯一的不足是編譯程序無(wú)法檢查引用print_report()函數(shù)時(shí)RPT_PARMS結(jié)構(gòu)的10個(gè)成員是否符合要求。
親,你說(shuō)的是函數(shù)重載吧?
重構(gòu)是指代碼重構(gòu),就是重新寫代碼,因?yàn)榍懊鎸戇^(guò)一次,所以再寫的時(shí)候可能會(huì)有更好的想法,就像做作業(yè)檢查一樣。
函數(shù)重載是指函數(shù)名一樣,參數(shù)不一樣,即為函數(shù)重載:
int func(int a){....},int func(char *p){....}和int func(int *p){....}都是它的重載函數(shù)
不知道你明白沒有
網(wǎng)站欄目:c語(yǔ)言函數(shù)參數(shù)過(guò)多重構(gòu),c語(yǔ)言形參重定義
瀏覽地址:http://chinadenli.net/article10/dsieigo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、商城網(wǎng)站、網(wǎng)站導(dǎo)航、企業(yè)建站、、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)