1 / 24

Functions

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

abrial
Télécharger la présentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Standard Library Functions User-defined Functions

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

  3. Standard Library Function • Mathematic <math.h>sin, cos, tan, pow, sqrt, …… • Character Manipulation <ctype.h>isalpha, isdigit, isupper, islower, … • String Manipulation <string.h>strcpy, strcat, strcmp, strlen, …… • Random Number <stdlib.h>randomize, random

  4. Mathematic Library Functions • <math.h> must be included • double data type is used in calculation • Trionometic 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 <stdio.h> #include <math.h> 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 <stdio.h> #include <math.h> 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 • <ctype.h> 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 • <string.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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” และกลับไปทำงานในข้อ 24. หากตัวเลขที่ผู้ใช้ป้อนมีค่าน้อยกว่า ให้แสดงข้อความว่า“Less than” และกลับไปทำงานในข้อ 25. หากผู้ใช้ทายค่าตัวเลขถูกต้อง ให้แสดงข้อความว่า “You’re the winner” และจำนวนครั้งของการทาย6. จบการทำงาน

More Related