function c
DESCRIPTION
การเขียนฟังก์ชันในภาษา CTRANSCRIPT
![Page 1: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/1.jpg)
Chapter Chapter 44FunctionFunction
Mr.Warawut Khangkhan
Twitter: http://twitter.com/awarawut
Facebook: http://www.facebook.com/AjWarawut
E-Mail: [email protected]
Mobile: 083-0698-410Mobile: 083-0698-410
![Page 2: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/2.jpg)
ContentsContents
� Binary function or Standard function.
� User defined function.
� Format function declaration.
� Type of functions.� Type of functions.
� The score of variables.
Mr.Warawut Khangkhan Chapter 4 Function 2
![Page 3: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/3.jpg)
FUNCTIONFUNCTION
Mr.Warawut Khangkhan Chapter 4 Function 3
![Page 4: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/4.jpg)
FunctionFunction
� ����������� ���������� ��������� main( ) ������������������������� ���� !���"��������� ����#$�%��&���
� ��������'�(�)� C �����*&����� 2 ��+�(� �,$ �-������������ (Library Function) .�,$�����$��$����.�/���� ��������'�(�)� �����*&����� ��+�(� �,$ �-������������ (Library Function) .�,$�����$��$����.�/���� ������������0�� &-+�����������12�'�������#/3��$� (User Defined Function)
Mr.Warawut Khangkhan Chapter 4 Function 4
![Page 5: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/5.jpg)
BINARY FUNCTION OR BINARY FUNCTION OR STANDARD FUNCTIONSTANDARD FUNCTIONSTANDARD FUNCTIONSTANDARD FUNCTION
Mr.Warawut Khangkhan Chapter 4 Function 5
![Page 6: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/6.jpg)
Binary function or Standard Binary function or Standard functionfunction� �������������������'.�4��$�����&�-(�)� C� �4,�$'�����'���������� %������������4,3�0�� ���� ��������� ��5����5"�6�����, ��������� �.����� �������#�$��� .�,$�������� �������������-� .�,$�������� �������������-� �������
Mr.Warawut Khangkhan Chapter 4 Function 6
![Page 7: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/7.jpg)
Binary function or Standard Binary function or Standard function (Cont.)function (Cont.)� �-������������'�(�)� C +���$�2�'���-������!- .h .�,$���������� header file ������������ ����&��-+��+�(� + �����$�2�'� header file ������� ���� ������������������� ��5����5"�6����� +���$�2�'���-��,�$ math.h .�,$��-����������� �����#�$��� +���$�2�'���-��,�$ string.h� ��5����5"�6����� +���$�2�'���-��,�$ .�,$��-����������� �����#�$��� +���$�2�'���-��,�$ string.h
Mr.Warawut Khangkhan Chapter 4 Function 7
![Page 8: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/8.jpg)
Binary function or Standard Binary function or Standard function (Cont.)function (Cont.)� ��������'���-������������ +��$��#���� �����#include <header file> ��������.�#$�%��&��� �4,�$'.���&�-(�)� C �#��' �� (��'�%��&���#$��������������'���-���������������$�2�'� header file ����-�� &-+�-������'�������������(��'�%��&��� +��$�������,�$4��$������'���-���������������$�2�'� ����-�� &-+�-������'�������������(��'�%��&��� +��$�������,�$4��$�� �.����&��'.�*2���$�����2�&#$��-��������������3�
Mr.Warawut Khangkhan Chapter 4 Function 8
![Page 9: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/9.jpg)
Example Example lib_func.clib_func.c
#include <stdio.h>#include <math.h>
int main( ) {int x, y;int x, y;printf(“Enter number: “);scanf(“%d”, &x);y = sqrt(x);printf(“Square root of %d = %d”, x, y);
system(“PAUSE”);return 0;
}Mr.Warawut Khangkhan Chapter 4 Function 9
![Page 10: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/10.jpg)
USER DEFINED USER DEFINED FUNCTIONFUNCTIONFUNCTIONFUNCTION
Mr.Warawut Khangkhan Chapter 4 Function 10
![Page 11: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/11.jpg)
������������������ ����������� ���������������(User defined function)(User defined function)� �-������������'�(�)� C ��������������������#/3����4,�$� ����4,3�0������� 8/��'������3�$� +������-���������������� �������$������������$���� �����3� (�)� C /�'.�����������������#/3���'���$����
� #�$�� �,$ '���5�������!�� ����������$�'������$� ��������*�#���� #�$�� �,$ '���5�������!�� ����������$�'������$� ��������*�#�����������#/3������3�����&-+�����*�����'������$����������$���� %�������$������-��#���'.��
Mr.Warawut Khangkhan Chapter 4 Function 11
![Page 12: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/12.jpg)
User defined function (ContUser defined function (Cont.).)
� ����������������#/3���'�����'�%��&��� �������&������ ����#$�%��&���$$���������-��� � �'.�����&-+�+����$����� �$&���#%��&��� %���94�+%��&��������#���'.:� .�,$��12��#���������.-����
Mr.Warawut Khangkhan Chapter 4 Function 12
![Page 13: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/13.jpg)
FORMAT FUNCTION FORMAT FUNCTION DECLARATIONDECLARATIONDECLARATIONDECLARATION
Mr.Warawut Khangkhan Chapter 4 Function 13
![Page 14: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/14.jpg)
����� ������� � �������������������� � ������������������ CC(Format function declaration)(Format function declaration)
type function-name ( type arg-1, type arg-2, … ) {
local variable declaration;
statement-1;statement-2;statement-2;statement-3;…statement-n;
return ( value );}
Mr.Warawut Khangkhan Chapter 4 Function 14
![Page 15: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/15.jpg)
Example Example ex_func.cex_func.c
#include <stdio.h>
void line( ) {int i;for (i=0; i<30; i++)
void main( ) {
line( );
printf(“Welcome to C Programming\n”);for (i=0; i<30; i++)
printf(“*”);printf(“\n”);
}
C Programming\n”);
line( );}
Mr.Warawut Khangkhan Chapter 4 Function 15
![Page 16: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/16.jpg)
TYPE OF FUNCTIONSTYPE OF FUNCTIONS
Mr.Warawut Khangkhan Chapter 4 Function 16
![Page 17: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/17.jpg)
� ���� �������������������������(Type of functions)(Type of functions)��+�(�#$��������� �����*&����� 3 ��+�(� �,$� ���������������������/������� ����������������������#����'���������� �����������������������-�$$� ����������� �����������������������-�$$� ����������
Mr.Warawut Khangkhan Chapter 4 Function 17
![Page 18: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/18.jpg)
������������������������������������������//������������
� .�������� ��������'������������+�(���3�����$����$����"����� (Argument) '�� �#����'.����������� &-+�������� +� ����%����������������1--�4;�'�� $$��� 8/��*,$�������������������"�����#��������������!� ���'.:�&-����������������'������������&���1-����������������"�����#��������������!� ���'.:�&-����������������'������������&���1-
Mr.Warawut Khangkhan Chapter 4 Function 18
![Page 19: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/19.jpg)
������������������������������������������
� .���*/� ������������-� +�����'�������$������������$����"����� (Argument) ��'.�������������3���� 8/��$����"���������������$�������#�$� �.��#$��������� ���� ��������� �.��'.����$����"����� 3 ������ int, float &-+int �-������'��������������� +��$����#�$�2- 8/��$� +$�2�'�int �-������'��������������� +��$����#�$�2- 8/��$� +$�2�'��2�&#$���&�� �"4 �� .�,$������������� &����$�������"�#�$�2- int, float &-+ int ��������- ���
Mr.Warawut Khangkhan Chapter 4 Function 19
![Page 20: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/20.jpg)
�����������������������-�$$� ���������������������������������-�$$� ����������
� ����������������������1--�4;� ����� �����-�$$��� ������������� 8/�����'.:�&-�����������+�(���3��� +'���������+�(������$�� ��5��� '���������'���������������+�(����������������-� ��� +��$��������&���4,�$�$�����1--�4;� ����������'�����������������������-� ��� +��$��������&���4,�$�$�����1--�4;� ����������'�������������
Mr.Warawut Khangkhan Chapter 4 Function 20
![Page 21: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/21.jpg)
�����������������������-�$$� ���������������������������������-�$$� ����������(Cont.)(Cont.)� �2�&
variable = function-name ( arg-1, arg-2,
… );
� �������������-�$$� ���������� '.�'��� ����� return %��� �������������-�$$� ���������� '.�'��� ����� return %���#���������� ������!�����#$���������
Mr.Warawut Khangkhan Chapter 4 Function 21
![Page 22: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/22.jpg)
THE SCORE OF THE SCORE OF VARIABLESVARIABLESVARIABLESVARIABLES
Mr.Warawut Khangkhan Chapter 4 Function 22
![Page 23: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/23.jpg)
�������������� ���������!�� ���������!�� �� (The score of variables)(The score of variables)� ��&��(�)� C &�����#$�#�#$���������&-+���'����� %��&��$$����� 2 ��"� �,$◦ ��&�� global◦ ��&�� local ��&�� local
Mr.Warawut Khangkhan Chapter 4 Function 23
![Page 24: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/24.jpg)
��&����&�� globalglobal
� ��&�� global .�,$�����$��$����.�/���� ��&�� external ������&����+�(���������*�����'�� ����������'������(��'�%��&��� %������������&�� global +��$��#���� �������+��6�������������� �.�������"�����#$�%��&�����$ ��� �����4��%4��8��8$����������� %���������������(��'�����������+��6�������������� �.�������"�����#$�%��&�����$ ��� �����4��%4��8��8$����������� %���������������(��'���������'���������.�/��%���94�+
Mr.Warawut Khangkhan Chapter 4 Function 24
![Page 25: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/25.jpg)
��&����&�� global (Cont.)global (Cont.)
� #$�#����'�������&�� global ���,$ �-$���3�%��&��������*�����'��.�,$�#���� ������4,�$��-����&�-����#$���&����+�(� global ����������'������(��'�%��&���
Mr.Warawut Khangkhan Chapter 4 Function 25
![Page 26: Function C](https://reader034.vdocument.in/reader034/viewer/2022042601/546b8c55b4af9f662c8b4d64/html5/thumbnails/26.jpg)
��&����&�� locallocal
� ��&�� local +�����$��$����.�/���� ��&��automatic ������&����+�(���������#/3���(��'��������� #$�#����'�������&�� local �� +$�2�(��'�����������������#/3�������3� 8/��.�������� �����������*�����'�����.�,$��-���������&�� ����������$,����� &-+*������������.�,$��-���������&�� ����������$,����� &-+*��������������&�� local &-���"������3��,�$83 �����+.����������� �� +*,$��������-+��&����� ���������-���������&��'���������'���������.�/�� +�����1-��$��&�� local ���'���,�$�������'���������$,��
Mr.Warawut Khangkhan Chapter 4 Function 26