Lection 5function

451 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
451
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lection 5function

  1. 1. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 1 Õýðýãëýã÷èéí ôóíêö 1. Ñè õýëýýð õýðýãëýã÷èéí ôóíêö òîäîðõîéëîõ Ñè õýëýíä ôóíêöóóäûã ñòàíäàðò áà õýðýãëýã÷èéí ãýæ 2 àíãèëäàã. Ñòàíäàðò ôóíêöóóä íü óðüä÷èëàí òîäîðõîéëîãäñîí (çîõèîãäñîí) ôóíêöóóä áàéäàã áà *.h ôàéëóóäàä òîäîðõîéëîãääîã. Õàðèí õýðýãëýã÷èéí ôóíêöèéã ïðîãðàìì çîõèîã÷ çîõèîíî. Ôóíêö íü ïðîãðàììä îëîí äàâòàí õèéãäýõ ¿éëäë¿¿äèéã áàãàñãàæ, ¿éëäë¿¿äèéã á¿ëýãëýæ ºãäºã. 2. Õýðýãëýã÷èéí ôóíêö çàðëàõ Áè÷èõ õýëáýð: <áóöààõ óòãûí òºðºë> <ÔóíêöÍýð>(<òºðºë> <õóâüñàã÷> , <òºðºë> <õóâüñàã÷> . . .) { <Ôóíêöûí áèå> } 1. Õýðýâ ôóíêö óòãà áóöààõ øààðäëàãàòàé áîë ôóíêöûí íýðèéí ºìíº òóõàéí ôóíêöýýñ áóöààõ óòãûí òºðëèéã áè÷èæ ºãíº. Õýðýâ óòãà áóöààäàãã¿é áîë ºìíº íü void ò¿ëõ¿¿ð ¿ãèéã áè÷èæ ºãíº ýñâýë åðººñºº ò¿ëõ¿¿ð ¿ã áè÷èõã¿é. 2. Õýðýâ ôóíêö ðóó àëèâàà óòãà äàìæäàã áîë ò¿¿íèéã õ¿ëýýæ àâàõ õóâüñàã÷äûã àðãóìåíòóóä ãýæ íýðëýäýã áà ôóíêöûí íýðèéí àðä ( ) õààëòàíä òºðºëòýé íü õàìò áè÷èæ ºãíº. Õýðýâ ôóíêö óòãà õ¿ëýýí àâäàãã¿é áîë ( ) õààëòàíä þó ÷ áè÷èõã¿é. 3. Ôóíêöûí áèåèéã { } õààëòàíä áè÷íý. Æèøýý : int Max(int a, int b) { int k; k=(a>b) ? a : b; printf(“ Max= %d”, k); return k; } Áóöààõ óòãà Àðãóìåíòóóä Ëîêàëü õóâüñàã÷ Óòãà áóöààõ ¿éëäýë
  2. 2. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 2 3. Óòãà áóöààäàãã¿é ôóíêöûã çàðëàõ 1. void ò¿ëõ¿¿ð ¿ã àøèãëàõ void Sum(int a, int b) { printf(“ Sum = %d”, a+b); } 2. Ò¿ëõ¿¿ð ¿ã àøèãëàõã¿é Sum(int a, int b) { printf(“ Sum = %d”, a+b); } Ôóíêö óòãà áóöààõã¿é òîõèîëäîëä ò¿¿íèéã èëýðõèéëýëä àøèãëàæ áîëîõã¿é. Ê=Sum(a,b)*10+123;  àëäàà printf(“%d”, Sum(a,b));  àëäàà 4. Ôóíêöýýñ óòãà áóöààõ îïåðàòîð Ñè õýëýíä ôóíêöýýñ óòãà áóöààõäàà return ò¿ëõ¿¿ð ¿ãèéã õýðýãëýäýã. Áè÷èãäýõ õýëáýð : return [ ( ] <èëýðõèéëýë> [ ) ]; Æèøýý : return a+b; return (a+b); return a; return (a); Return ¿éëäýë ã¿éöýòãýãäýõýä óäèðäëàãà òóõàéí ôóíêöýýñ ãàðíà. ªºðººð õýëáýë íýã áëîêò ò¿¿íèé àðä áè÷èãäñýí ¿éëäë¿¿ä áèåëýãäýõã¿é. Æèøýý : int Sum(int a, int b) { int k; k=a+b; return k;
  3. 3. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 3 printf(“Sum=%d”,k);  ¯éëäýë õýçýý ÷ áèåëýãäýõã¿é } 5. Àðãóìåíòã¿é ôóíêöèéã çàðëàõ Sum(void) { printf(“ Sum = %d”, a+b); } Sum() { printf(“ Sum = %d”, a+b); } 6. Ôóíêöûã äóóäàõ Ôóíêöûã ò¿¿íèé íýðýýð, àðãóìåíòèéã íü äàìæóóëàí äóóäíà. Æèøýý : Sum(); /* Àðãóìåíòã¿é ôóíêö äóóäàõ */ Sum(5,7); /* Òîãòìîë óòãûã ôóíêöýä äàìæóóëàõ */ Sum(i,k); /* Õóâüñàã÷äûí óòãûã äàìæóóëàõ */ i=Sum(5,7); /* Óòãà áóöààäàã ôóíêöûã àøèãëàõ */ i=Sum(i,k); i=m+Sum(5,k); /* Èëýðõèéëýëä àøèãëàõ */ Æèøýý : 2 òîîíû íèéëáýð îëäîã ïðîãðàìì çîõèî. #include <stdio.h> int Sum(int a, int b) { return a+b; } main() { int i,j; scanf(“%d %d”,&i,&j); printf(“Sum=%d”,Sum(i,j)); } 7. Õýðýãëýã÷èéí ôóíêöóóäûã áóñàä ôóíêöýýñ õýðýãëýõ íü Ñè õýëýíä òóõàéí ôóíêö äîòî𠺺ð õýðýãëýã÷èéí ôóíêöèéã äóóäàõ áîë çºâõºí ººðººñºº ºìíº òîäîðõîéëîãäñîí ôóíêöèéã äóóäàæ áîëíî. Æèøýý :
  4. 4. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 4 int Sum(int a, int b) { return a+b; } main() { /* Sum ôóíêö main ôóíêöýýñ ºìíº òîäîðõîéëîãäñîí ó÷èð ò¿¿íèéã main ôóíêöýýñ äóóäàæ áîëíî. */ printf(“Sum = %d”, Sum(5,7)); /* Max ôóíêö main ôóíêöýýñ ºìíº òîäîðõîéëîãäîîã¿é ó÷èð ò¿¿íèéã main ôóíêöýýñ äóóäàæ áîëîõã¿é. */ printf(“Max = %d”,Max(5,7)); } int Max(int a, int b) { return (a>b) ? a : b; } Ýíý òîõèîëäîëä òóõàéí ôóíêöèéã äóóäàõûí òóëä ººðººñºº ºìíº çàðëàæ ( ôóíêöûí çºâõºí òîäîðõîéëîëòûã íü çàðëàíà ) ºãäºã. Èíãýæ çàðëàõäàà òóõàéí ôóíêö ð¿¿ äàìæèõ àðãóìåíòûí áîëîí áóöààõ óòãûí òºðëèéã çààæ ºãäºã. Áè÷èõ õýëáýð : <áóöààõ òºðºë> < ÔóíêöÍýð>(<Àðãóìåíòèéí òºðºë> , . . .); Æèøýý : int Sum(int a, int b) { return a+b; } int Max(int,int); main() { /* Sum ôóíêö main ôóíêöýýñ ºìíº òîäîðõîéëîãäñîí ó÷èð ò¿¿íèéã main ôóíêöýýñ äóóäàæ áîëíî. */ printf(“Sum = %d”, Sum(5,7)); /* Max ôóíêö main ôóíêöýýñ ºìíº òîäîðõîéëîãäîîã¿é ó÷èð ò¿¿íèéã main ôóíêöýýñ äóóäàæ áîëîõã¿é. */ printf(“Max = %d”,Max(5,7)); } int Max(int a, int b)
  5. 5. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 5 { return (a>b) ? a : b; } 8. Ôóíêöèéí äîòîîä õóâüñàã÷ Ǻâõºí òóõàéí ôóíêö äîòðîî ë õýðýãëýãäýõ ëîêàëü õóâüñàã÷èéã äîòîîä õóâüñàã÷èä ãýíý. ªºð ººð ôóíêöóóäûí äîòîîä õóâüñàã÷èä áèå áèåäýý íºëºº ¿ç¿¿ëýõã¿é. Óäèðäëàãà òóõàéí ôóíêöýä øèëæèõýä ýäãýýð õóâüñàã÷èä ñàíàõ îéä ¿¿ñýõ áà ôóíêöýýñ ãàðàõàä ñàíàõ îéãîîñ óñòàæ ¿ã¿é áîëíî. ª.õ òóõàéí ôóíêöèéí àìüäðàõ õóãàöààíä îðøèí òîãòíîíî. Æèøýý : int Max(int a, int b) { int k; k=(a>b) ? a : b; return k; } Æèøýý : #include <stdio.h> int k; Sum(int a, int b) { int k; k=a+b; printf(“k=%d”,k); } main() { k=1; printf(“k=%d”,k); /* k = 1 */ Sum(2,2); /* k = 4 */ printf(“k=%d”,k); /* k = 1 */ } Äîòîîä õóâüñàã÷
  6. 6. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 6 9. Ñòàòèê õóâüñàã÷ (static) Óòãà òóõàéí ïðîãðàììûí àìüäðàëûí õóãàöààíä õàäãàëàãääàã õóâüñàã÷èéã ñòàòèê õóâüñàã÷ ãýíý. Ôóíêö äîòîð ñòàòèê õóâüñàã÷èéã çàðëàæ áîëäîã . Ýíý òîõèîëäîëä òóõàéí ôóíêöèéã äóóäàõ áîëãîíä õóâüñàã÷èéã øèíýýð ¿¿ñãýõã¿é õàðèí ºìíºõ ¿éëäëèéí ¿ð ä¿íãýýð òîãòîîãäñîí óòãûã àâíà. Áè÷èãäýõ õýëáýð : static <òºðºë> <õóâüñàã÷>; Æèøýý : #include <stdio.h> Count() { static int k=1; printf(“k=%dn”, k++); } main() { Count(); /* k = 1 */ Count(); /* k = 2 */ Count(); /* k = 3 */ Count(); /* k = 4 */ Count(); /* k = 5 */ } Æèøýý : #include <stdio.h> Multiple(int a) { static int k=1; printf(“k=%dn”, k); k*=a; } main()
  7. 7. “Ïðîãðàì÷ëàëûí õýë” Ôóíêö 7 { int i; for(i=1; i<=5; i++) Multiple(2); } /* k=1 k=2 k=4 k=8 k=16 */

×