function c

26
Chapter Chapter 4 Function Function Mr.Warawut Khangkhan Twitter: http://twitter.com/awarawut Facebook: http://www.facebook.com/AjWarawut E-Mail: [email protected] Mobile: 083-0698-410

Upload: warawut

Post on 18-Nov-2014

998 views

Category:

Sports


1 download

DESCRIPTION

การเขียนฟังก์ชันในภาษา C

TRANSCRIPT

Page 1: Function C

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

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

FUNCTIONFUNCTION

Mr.Warawut Khangkhan Chapter 4 Function 3

Page 4: Function C

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

BINARY FUNCTION OR BINARY FUNCTION OR STANDARD FUNCTIONSTANDARD FUNCTIONSTANDARD FUNCTIONSTANDARD FUNCTION

Mr.Warawut Khangkhan Chapter 4 Function 5

Page 6: Function C

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

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

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

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

USER DEFINED USER DEFINED FUNCTIONFUNCTIONFUNCTIONFUNCTION

Mr.Warawut Khangkhan Chapter 4 Function 10

Page 11: Function C

������������������ ����������� ���������������(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

User defined function (ContUser defined function (Cont.).)

� ����������������#/3���'�����'�%��&��� �������&������ ����#$�%��&���$$���������-��� � �'.�����&-+�+����$����� �$&���#%��&��� %���94�+%��&��������#���'.:� .�,$��12��#���������.-����

Mr.Warawut Khangkhan Chapter 4 Function 12

Page 13: Function C

FORMAT FUNCTION FORMAT FUNCTION DECLARATIONDECLARATIONDECLARATIONDECLARATION

Mr.Warawut Khangkhan Chapter 4 Function 13

Page 14: Function C

����� ������� � �������������������� � ������������������ 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

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

TYPE OF FUNCTIONSTYPE OF FUNCTIONS

Mr.Warawut Khangkhan Chapter 4 Function 16

Page 17: Function C

� ���� �������������������������(Type of functions)(Type of functions)��+�(�#$��������� �����*&����� 3 ��+�(� �,$� ���������������������/������� ����������������������#����'���������� �����������������������-�$$� ����������� �����������������������-�$$� ����������

Mr.Warawut Khangkhan Chapter 4 Function 17

Page 18: Function C

������������������������������������������//������������

� .�������� ��������'������������+�(���3�����$����$����"����� (Argument) '�� �#����'.����������� &-+�������� +� ����%����������������1--�4;�'�� $$��� 8/��*,$�������������������"�����#��������������!� ���'.:�&-����������������'������������&���1-����������������"�����#��������������!� ���'.:�&-����������������'������������&���1-

Mr.Warawut Khangkhan Chapter 4 Function 18

Page 19: Function C

������������������������������������������

� .���*/� ������������-� +�����'�������$������������$����"����� (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

�����������������������-�$$� ���������������������������������-�$$� ����������

� ����������������������1--�4;� ����� �����-�$$��� ������������� 8/�����'.:�&-�����������+�(���3��� +'���������+�(������$�� ��5��� '���������'���������������+�(����������������-� ��� +��$��������&���4,�$�$�����1--�4;� ����������'�����������������������-� ��� +��$��������&���4,�$�$�����1--�4;� ����������'�������������

Mr.Warawut Khangkhan Chapter 4 Function 20

Page 21: Function C

�����������������������-�$$� ���������������������������������-�$$� ����������(Cont.)(Cont.)� �2�&

variable = function-name ( arg-1, arg-2,

… );

� �������������-�$$� ���������� '.�'��� ����� return %��� �������������-�$$� ���������� '.�'��� ����� return %���#���������� ������!�����#$���������

Mr.Warawut Khangkhan Chapter 4 Function 21

Page 22: Function C

THE SCORE OF THE SCORE OF VARIABLESVARIABLESVARIABLESVARIABLES

Mr.Warawut Khangkhan Chapter 4 Function 22

Page 23: Function C

�������������� ���������!�� ���������!�� �� (The score of variables)(The score of variables)� ��&��(�)� C &�����#$�#�#$���������&-+���'����� %��&��$$����� 2 ��"� �,$◦ ��&�� global◦ ��&�� local ��&�� local

Mr.Warawut Khangkhan Chapter 4 Function 23

Page 24: Function C

��&����&�� 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

��&����&�� global (Cont.)global (Cont.)

� #$�#����'�������&�� global ���,$ �-$���3�%��&��������*�����'��.�,$�#���� ������4,�$��-����&�-����#$���&����+�(� global ����������'������(��'�%��&���

Mr.Warawut Khangkhan Chapter 4 Function 25

Page 26: Function C

��&����&�� locallocal

� ��&�� local +�����$��$����.�/���� ��&��automatic ������&����+�(���������#/3���(��'��������� #$�#����'�������&�� local �� +$�2�(��'�����������������#/3�������3� 8/��.�������� �����������*�����'�����.�,$��-���������&�� ����������$,����� &-+*������������.�,$��-���������&�� ����������$,����� &-+*��������������&�� local &-���"������3��,�$83 �����+.����������� �� +*,$��������-+��&����� ���������-���������&��'���������'���������.�/�� +�����1-��$��&�� local ���'���,�$�������'���������$,��

Mr.Warawut Khangkhan Chapter 4 Function 26