參數(shù)的多少與你的問題的復雜度和解決問題的方式都有一定的關聯(lián)。如果一個函數(shù)處理的問題所需要的參數(shù)就是那么多的話,就是合適的,不必修改。
站在用戶的角度思考問題,與客戶深入溝通,找到無極網(wǎng)站設計與無極網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站建設、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬主機、企業(yè)郵箱。業(yè)務覆蓋無極地區(qū)。
fun函數(shù)聲明錯誤,正確是?char fun(char a[20], char b);
int b; 變量 b重復定義,此句去掉。
fun(a[20], e);? 函數(shù)參數(shù)傳遞錯誤,正確的是?fun(a, e);
正確代碼如下:
#include?stdio.h
#includestring.h
int?main()
{
char?fun(char?a[20],?char?b); //?函數(shù)聲明錯誤
char?a[20],?e,?b[200];
//?int?b; //?b重復定義了,此句去掉
gets(a);
scanf("%c",?e);
fun(a,?e); //?a?參數(shù)傳遞錯誤,正確?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];
}
一個函數(shù)的參數(shù)的數(shù)目沒有明確的限制,但是參數(shù)過多(例如超過8個)顯然是一種不可取的編程風格。參數(shù)的數(shù)目直接影響調(diào)用函數(shù)的速度,參數(shù)越多,調(diào)用函數(shù)就越慢。另一方面,參數(shù)的數(shù)目少,程序就顯得精練、簡潔,這有助于檢查和發(fā)現(xiàn)程序中的錯誤。因此,通常應該盡可能減少參數(shù)的數(shù)目,如果一個函數(shù)的參數(shù)超過4個,你就應該考慮一下函數(shù)是否編寫得當。 如果一個函數(shù)不得不使用很多參數(shù),你可以定義一個結(jié)構來容納這些參數(shù),這是一種非常好的解決方法。在下例中,函數(shù)print_report()需要使用10個參數(shù),然而在它的說明中并沒有列出這些參數(shù),而是通過一個RPT_PARMS結(jié)構得到這些參數(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; } 上例唯一的不足是編譯程序無法檢查引用print_report()函數(shù)時RPT_PARMS結(jié)構的10個成員是否符合要求。
親,你說的是函數(shù)重載吧?
重構是指代碼重構,就是重新寫代碼,因為前面寫過一次,所以再寫的時候可能會有更好的想法,就像做作業(yè)檢查一樣。
函數(shù)重載是指函數(shù)名一樣,參數(shù)不一樣,即為函數(shù)重載:
int func(int a){....},int func(char *p){....}和int func(int *p){....}都是它的重載函數(shù)
不知道你明白沒有
網(wǎng)站欄目:c語言函數(shù)參數(shù)過多重構,c語言形參重定義
瀏覽地址:http://aaarwkj.com/article10/dsieigo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、商城網(wǎng)站、網(wǎng)站導航、企業(yè)建站、、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)