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

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

ครั้งที่ 8 Function.

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


งานนำเสนอเรื่อง: "ครั้งที่ 8 Function."— ใบสำเนางานนำเสนอ:

1 ครั้งที่ 8 Function

2 การเขียนโปรแกรมโดยทั่วไป
#include <stdio.h> void main() { printf(“…………………….”); if (………) printf(“…………”); else for(…………………) }

3 การเขียนโปรแกรมแบบมีฟังก์ชันย่อย
#include <stdio.h> void sub1() { if (………) printf(“…………”); else } void sub2() for(…………………) void main() printf(“…………………….”); sub1(); sub2(); ฟังก์ชันย่อย sub1 ฟังก์ชันย่อย sub2

4 ฟังก์ชันย่อยควรอยู่ตรงไหน? ทำได้ 2 แบบ
วางไว้ก่อนโปรแกรมหลัก ไม่ต้องประกาศ Function Prototype วางไว้หลังโปรแกรมหลัก ต้องประกาศ Function Prototype

5 วางไว้ก่อนโปรแกรมหลัก

6 วางไว้หลังโปรแกรมหลัก

7 สิ่งที่ต้องรู้จัก parameter (ตัวส่ง) argument (ตัวรับ)
ฟังก์ชัน main() จะเป็นผู้ส่ง parameter ไปยังฟังก์ชันย่อย argument (ตัวรับ) ฟังก์ชันย่อยจะทำการกำหนด argumentขึ้นเพื่อรองรับ parameter จาก main()

8 ประเภทของฟังก์ชัน ไม่มีการส่งค่า parameter มีการส่งค่า parameter
ไม่มีการส่งค่ากลับไปที่ main มีการส่งค่ากลับไปที่ main มีการส่งค่า parameter

9  ฟังก์ชันที่ไม่มีการส่งค่า parameter และไม่ส่งค่ากลับ main
Ex1 เขียน แบบที่ มี function prototype #include <stdio.h> void hello(); void main() { hello(); } void hello() printf(“Hello my student\n”); pritnf(“How are you today?”); #include <stdio.h> void hello() { printf(“Hello my student\n”); pritnf(“How are you today?”); } void main() hello();

10 Ex2 จงเขียนโปรแกรมย่อยชื่อ line() เพื่อแสดงเสร็จ ทั้งหมด 5 เสร็จ
#include<stdio.h> void line(); void main() { ผลรัน ===============

11 Ex3 : จงเขียนฟังก์ชันชื่อ find_area เพื่อรับค่ารัศมีและคำนวณหาพื้นที่วงกลม และฟังก์ชันชื่อ find_circum เพื่อรับค่ารัศมีและคำนวณหาความยาวเส้นรอบวงกลม ตัวอย่างผลรัน Select choice (1:Area, 2: Circum) : 1 Input Radius : 2 Result = 12.56 ************************************* Select choice (1:Area, 2: Circum) : 2 Input Radius : 4 Result = 25.12

12 #include<stdio.h>
#define pi 3.14 void main() { int choice; printf(“Select choice (1:Area, 2: Circum) :”); } find_area( ) { int r; float result; printf("Input Radius : "); void find_circum() printf("Input Radius : ");

13 ฟังก์ชันที่ไม่มีการส่งค่า parameter แต่ส่งค่ากลับ main
Ex4 #include <stdio.h> float triarea(void) { float base, high, area; printf(“Enter base : ”); scanf(“%f”,&base); printf(“Enter high : ”); scanf(“%f”,&high); area = base * high / 2; return(area) } void main() float area; area = triarea(); printf(“Triangle Area is %.2f\n”,area);

14 ฟังก์ชันที่มีการส่งค่า parameter และไม่มีการส่งค่ากลับไปที่ main
Ex5 #include<stdio.h> #include<conio.h> void calculate(int n1,int n2) { int result; result = n1+n2; printf("Result = %d",result); } void main() int num1,num2; printf("Please enter number1 : "); scanf("%d",&num1); printf("Please enter number2 : "); scanf("%d",&num2); calculate(num1,num2); Please enter number1: 10 Please enter number2: 5 Result = 15

15 Ex6 จงเขียนโปรแกรมเพื่อวนพิมพ์ข้อความ “BC322 is very easy” โดยที่
ฟังก์ชัน main() ทำหน้าที่ในการเรียกใช้ฟังก์ชัน print_string() โดยรับจำนวนรอบที่ต้องการให้ทำซ้ำ ฟังก์ชัน print_string() ทำหน้าที่ในการพิมพ์ข้อความดังกล่าว ตัวอย่างผลรัน Please enter line : 10 Line1 : BC322 is very easy Line2 : BC322 is very easy ……………………………….. Line10 : BC322 is very easy

16 Solution Ex6 #include<stdio.h>

17 ฟังก์ชันที่มีการส่งค่า parameter และมีการส่งค่ากลับไปที่ main
#include<stdio.h> #include<conio.h> int cal_and_show(int n1,int n2) { int sum; sum = n1+n2; return(sum); } void main() int result,num1,num2; printf("Please enter number1 : "); scanf("%d",&num1); printf("Please enter number2 : "); scanf("%d",&num2); result = cal_and_show(num1,num2); printf("Result = %d",result); Ex7 Please enter number1: 10 Please enter number1: 5 Result = 15

18 Ex8 จงเขียนโปรแกรมเพื่อรับตัวเลือก (choice) และตัวเลข (num)
ถ้าตัวเลือก = 1 ให้แสดงผลลัพธ์ของเลขยกกำลัง 2 ถ้าตัวเลือก = 2 ให้แสดงผลลัพธ์เป็นค่ารากที่ 2 ถ้าตัวเลือกอื่น ๆ ให้แสดงข้อความ “Menu Error!!!” กำหนดให้main() รับค่า choice และ num แล้วส่งค่าไปให้ฟังก์ชัน ฟังก์ชัน power หาค่ายกกำลังสอง แล้วคืนค่ากลับ main ฟังก์ชัน sqroot หาค่ารากที่สอง แล้วคืนค่ากลับ main ตัวอย่างผลรัน Enter number : 4 Please select choice (1 = power, 2 = square root) : 1 Result = ********************************************* Please select choice (1 = power, 2 = square root) : 2 Result = Please select choice (1 = power, 2 = square root) : 3 Menu Error!!!

19 Solution Ex8 #include<stdio.h> #include<math.h>
double power(double n); double sqroot(double n);

20

21 สรุป การเรียกใช้ฟังก์ชันทั้ง 4 แบบ
แบบที่ 1 void calculate() { ... } void main() caculate(); แบบที่ 2 int calculate(void) { int x; return(x); { int ans; ans=caculate(); printf(“Answer = %d”,ans);

22 แบบที่ 3 void calculate(float a) { ... } void main() float m; .... caculate(m); แบบที่ 4 float calculate(float a) { float x; x= a*2; return(x); float m,n; n=caculate(m); printf(“answer = “,n);

23 Ex9 จงเขียนโปรแกรมเพื่อทำการคำนวณเกรด (grade) ที่ได้จากคะแนนสอบ (score) โดยมีเงื่อนไขดังนี้ เกรด A คะแนน เกรด B คะแนน 70-79 เกรด C คะแนน 70-79 เกรด D คะแนน 50-59 เกรด F คะแนนน้อยกว่า 50 กำหนดให้ ฟังก์ชัน main() ทำหน้าที่รับคะแนน และแสดงผลเกรดที่ได้ ฟังก์ชัน cal_grade() ทำหน้าที่รับคะแนนมาจาก main() แล้วคำนวณเกรดแล้วส่งกลับไปแสดงผลที่ main() Enter score : 58.6 You get D !!! Enter score : 89.7 You get A !!! Enter score : 48 You get F !!!

24 Solution Ex9 #include<stdio.h>

25 Ex10 จงเขียนโปรแกรมเพื่อรับเลขจำนวนเต็ม 2 จำนวนแล้วหาค่าเฉลี่ยของตัวเลขทั้ง 2 จำนวนนั้น กำหนดให้ ฟังก์ชัน main() ทำหน้าที่รับตัวเลข 2 จำนวน และแสดงผล ฟังก์ชัน cal_avg() ทำหน้าที่คำนวณค่าเฉลี่ยแล้วส่งค่ากลับ ตัวอย่างผลรัน Enter a: 10 Enter b: 20 Average is 15.00

26 Solution Ex10

27 ประเภทของตัวแปร (Variable)
Local Variable คือตัวแปรที่ใช้เฉพาะภายในฟังก์ชันย่อยเท่านั้น Global Variable คือตัวแปรที่ใช้ได้ในทุกฟังก์ชันของโปรแกรม

28 ตัวอย่างการใช้ตัวแปร Global
#include <stdio.h> int num1,num2; float average; void avg() { average = (float)(num1+num2)/2; } void main() printf("Enter num1: "); scanf("%d", &num1); printf("Enter num2: "); scanf("%d", &num2); avg(); printf("Average is %.2f\n", average); getch();

29 หมายเหตุ การใช้ตัวแปร Global อาจทำให้สับสนได้ หากมีการเรียกใช้ตัวแปรนั้นในหลายฟังก์ชัน

30 Input Output #include<stdio.h> int x,y,z; void print_out1() {
printf("X = %d\n",x); printf("Y = %d\n",y); } void print_out2() x--; y--; z = x+y ; printf("Z = %d\n",z); void main() printf("Input X : "); scanf("%d",&x); printf("Input Y : "); scanf("%d",&y); print_out1(); print_out2(); Input Input X: 10 Input Y: 5 Output


ดาวน์โหลด ppt ครั้งที่ 8 Function.

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


Ads by Google