งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Functions Standard Library Functions User-defined Functions.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Functions Standard Library Functions User-defined Functions."— ใบสำเนางานนำเสนอ:

1 Functions Standard Library Functions User-defined Functions

2 Function 1.Standard Library Function - built-in function - header file must be included 2.User Defined Function - must be declared (function prototype) - defined in the program

3 Standard Library Function Mathematic sin, cos, tan, pow, sqrt, …… Character Manipulation isalpha, isdigit, isupper, islower, … String Manipulation strcpy, strcat, strcmp, strlen, …… Random Number randomize, random

4 Mathematic Library Functions must be included double data type is used in calculation Trionometic functionTrionometic function - sin(radian), cos(radian), tan(radian) - sinh(radian), cosh(radian), tanh(radian) radian = Pi-radian angle = Degree * (PI / 180)

5 Standard Library Function #include void main() { double rad = 30 * (3.141592654/180); printf(“cos 30 = %f\n”,cos(rad)); printf(“sin 30 = %f\n”,sin(rad)); printf(“tan 30 = %f\n”,tan(rad)); }

6 Mathematic Library Functions Power of number - pow(x, y) Square Root of number - sqrt(x)

7 Standard Library Function #include void main() { double a = 32, b = 2, c; c = pow(a,b); printf(“%.2f power by %.2f = %.2f\n”,a,b,c); printf(“sqrt. of %.2f = %.2f”,c,sqrt(c)); }

8 Character Manipulation Function must be included Checking type of character - isalpha(ch); => return 1 if ‘ch’ is character Checking type of number - isdigit(ch); => return 1 if ‘ch’ is number Checking for upper and lower character - isupper(ch); => return 1 if ‘ch’ is uppercase - islower(ch); => return 1 if ‘ch’ is lowercase

9 Standard Library Function void main() { char ch; printf(“Enter : “); ch = getche(); if(isalpha(ch)) { printf(“%c is a letter\n”,ch); printf(“isalpha(ch) = %d\n”,isalpha(ch)); } else { printf(“%c is not a letter\n”,ch); printf(“isalpha(ch) = %d\n”,isalpha(ch)); }

10 String Manipulation Function must be included Comparing 2 strings - strcmp(str1,str2); => return 0 if equal Copying a string to another string - strcpy(str1,str2); => copy str2 to str1 Concatenating 2 strings - strcat(str1,str2); => concatenate & store in str1 Finding string length - strlen(str); => return length of str

11 Standard Library Function if(strcmp(str1,str2)) { if ((strcmp(str1,str2))>0) printf(“str1 is greater than str2\n”); else printf(“str1 is less than str2\n”); } else printf(“Same string\n”);

12 User-defined Function returnType funcName(…); // Function Declaration void main() { ….// main program …. } … funcName(….) { …// Function body }

13 Types of Function 1. Function with has no Parameter void functionName(void) 2. Function with getting values void functionName(dataType1, dataType2,…) 3. Function with returning value dataType functionName(void) 4. Function with getting and returning values dataType functionName(dataType1,dataType2,…)

14 Function with no parameter #include void hello(void);// Declare function prototype void main(void) { printf(“This line is printed from main.\n”); hello(); printf(“End of program.”); } void hello(void) { printf(“This line is printed from function hello.\n”); }

15 Function with getting parameter #include void print_line(char); // Declare function prototype void main(void) { char ch = ‘_’; printf(“This line is printed from main.\n”); print_line(ch); printf(“\nEnd of program.”); } void print_line(char x) { int a; for(a = 1; a<=40; a++) printf(“%c”,x); }

16 Function with returning value #include int get_int(void); // Declare function prototype void main(void) { int num1, num2; num1 = get_int(); num2 = get_int(); printf(“%d + %d = %d\n”,num1,num2,num1+num2); printf(“End of program.”); } int get_int(void) { int num; printf(“Please enter an integer number : “); scanf(“%d”,&num); return num; }

17 Function with getting and returning values #include int sum_int(int, int);// Declare function prototype void main(void) { int x = 10, y = 20, sum; sum = sum_int(x,y); printf(“%d + %d = %d\n”,x,y,sum); printf(“End of program.”); } int sum_int(int a, int b) { return a+b; }

18 Function with Parameter as a Function #include int get_int(void); int sum_int(int, int); void main(void) { int x, y, sum; x = get_int(); y = get_int(); sum = sum_int(x,sum_int(x,y)); printf(“sum = %d\n”,sum); printf(“End of program.”); }

19 Function with Parameter as a Function int get_int(void) { int num; printf(“Please enter an integer number : “); scanf(“%d”,&num); return num; } int sum_int(int a, int b) { return a+b; }

20 Passing Parameters There are 2 ways to pass parameters to a function 1. Pass by value 2. Pass by reference

21 Passing by Value void func(int, int); void main( ) { int x = 10, y = 20;........ func(x, y); printf(“X = %d, y = %d\n\n”,x,y); } void func(int x, int y) { x += 20;y *= 5; printf(“X = %d, y = %d\n\n”,x,y); } The output : x = 30, y = 100 x = 10, y = 20

22 Passing by Reference void func(int *, int *); void main( ) { int x = 10, y = 20;................. func(&x, &y); printf(“X = %d, y = %d\n\n”,x,y); } void func(int *a, int *b) { *a += 20;*b *= 5; printf(“*a = %d, *b = %d\n\n”,*a, *b); } The output : * a = 30, * b = 100 x = 30, y = 100

23 Assignment 1. เขียนฟังก์ชั่นเพื่อทำการตรวจสอบ ค่าที่รับเข้ามาจากผู้ใช้ ผ่านคีย์บอร์ดว่าอยู่ในช่วง 0 – 100 หรือไม่ โดยส่งค่า 1 กลับไป หากข้อมูลมีค่า ตามที่กำหนด และส่งค่า 0 กลับไป หากข้อมูลไม่ได้ มีค่าตามที่กำหนด 2. เขียนฟังก์ชั่นเพื่อทำหน้าที่สลับค่า ข้อมูลให้กับตัวแปร 2 ตัวที่ถูกส่งผ่านเป็นพารามิเตอร์ ของฟังก์ชั่น

24 Assignment เขียนโปรแกรมเกมทายค่าตัวเลข มี รายละเอียดดังนี้ 1. สุ่มค่าตัวเลข 1 ตัว (0-999) 2. รับค่าตัวเลขการทายจากผู้ใช้ 3. หากตัวเลขที่ผู้ใช้ป้อนมีค่ามากกว่า ให้ แสดงข้อความว่า “Greater than” และกลับไปทำงานใน ข้อ 2 4. หากตัวเลขที่ผู้ใช้ป้อนมีค่าน้อยกว่า ให้ แสดงข้อความว่า “Less than” และกลับไปทำงานในข้อ 2 5. หากผู้ใช้ทายค่าตัวเลขถูกต้อง ให้ แสดงข้อความว่า “You’re the winner” และจำนวนครั้ง ของการทาย 6. จบการทำงาน


ดาวน์โหลด ppt Functions Standard Library Functions User-defined Functions.

งานนำเสนอที่คล้ายกัน


Ads by Google