1 / 8

LAB # 6 Function

Computer Programming 1. LAB # 6 Function. ประกาศฟังก์ชันชื่อ func1 ไม่มีการ return ค่า และมี parameter int a. void main () { int x = 5; cout << " x = " << x << " a = " << a; func1 ( x ) ; cout << " x = " << x << " a = " << a;

ifama
Download Presentation

LAB # 6 Function

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. Computer Programming 1 LAB # 6Function

  2. ประกาศฟังก์ชันชื่อ func1 ไม่มีการ return ค่า และมี parameter int a void main(){ int x = 5; cout <<" x = " << x << " a = " << a; func1(x); cout << " x = " << x << " a = " << a; x = func2(); cout << " x = " << x << " a = " << a; } • ข้อ 1 ให้เติมช่องว่างในโปรแกรมให้ครบ และลองศึกษาดูว่า เมื่อเรียกฟังก์ชัน func1 และ func2 ว่าผลลัพธ์ที่ได้เป็นอย่างไร #include <iostream.h> int a = 20; { int x; x = a*2; cout <<" x = " << x << " a = " << a; } { int x; cout <<" x = " << x << " a = " << a; x = a*5; cout <<" x = " << x << " a = " << a; } ประกาศฟังก์ชันชื่อ func2 มีการ return ค่าชนิด int และไม่มี parameter Lab6-1.cpp return ตัวแปร x

  3. ข้อ 2 เป็นการเขียนโปรแกรมที่มีการทำงานเป็นดังนี้ 1. มีฟังก์ชันรับข้อมูล เพื่อรับข้อมูลตัวแรก 2. มีฟังก์ชันรับข้อมูล เพื่อรับข้อมูลตัวที่สอง 3. มีฟังก์ชันบวกเลข เพื่อทำการคำนวณ 4. มีฟังก์ชันแสดงผล เพื่อแสดงผลลัพธ์จากการคํานวณ 5. ในฟังก์ชัน main เป็นการทำงานโดยมีเรียกใช้ฟังก์ชันที่กล่าวมานี้ Lab6-2.cpp

  4. ข้อ 2 จงเติมโค้ดในช่องว่างที่ทำให้โปรแกรมสามารถทำงานได้แล้วลองสังเกตการทำงานของโปรแกรมว่าการทำงานของ function เป็นอย่างไร return ตัวแปร x #include <iostream.h> float InputFloat ( ) { float x; cout << " Input real value : " ; cin >> x ; } • ประกาศตัวแปร parameter • x เป็น float • y เป็น float float SumFloat () { return ( x + y ); } ประกาศฟังก์ชันชื่อ PrintOut ไม่มีการ return ค่า และมี parameter float x { cout << "\nResult of sum is : "<< x; } เรียกฟังก์ชัน InputFloat และ เมื่อ return ค่ากลับมาเก็บในตัวแปร a2 void main ( ) { float a1, a2, sumVal; a1 = InputFloat( ); sumVal = PrintOut (); } เรียกฟังก์ชั่น SumFloat และ ส่งค่าตัวแปร a1 a2 เป็น parameter ส่งตัวแปร sumVal เป็น parameter ของฟังก์ชัน PrintOut

  5. ข้อ 3 จงเติมโค้ดในโปรแกรมให้สมบูรณ์ • โปรแกรมเพื่อคำนวณปริมาณสีที่ใช้ในการทาห้อง 1 ห้อง โดยให้ผู้ใช้ระบุความกว้าง ความยาว และความสูงของห้องมีหน่วยเป็นฟุต (ไม่พิจารณาประตูหน้าต่าง) กำหนดให้สี 1 แกลลอนทาพื้นที่ได้ 250 ตร.ฟุต ให้โปรแกรมหาว่าจะต้องใช้สีกี่แกลลอน • กำหนดให้ฟังก์ชัน input() สำหรับรับข้อมูลความกว้าง ความยาว ความสูง • กำหนดให้ฟังก์ชัน area() เพื่อคำนวณพื้นที่ กว้าง * สูง หรือ ยาว คูณสูง และมีการ return ผลลัพธ์การคำนวณกลับ • กำหนดให้ฟังก์ชัน gallon() เพื่อคำนวณปริมาณสีที่ต้องใช้จากข้อมูลพื้นที่ผนังห้องทั้งหมด Lab6-3.cpp

  6. int area(int a, int b){ return(a*b); } int main(){ float result; //first call function input width length height input(); result = gallon(); cout <<"\nUse : " << result << "gallons"; } #include <iostream.h> int width; int length; int height; void input(){ cout <<"Please Enter width:"; cin >> width; cout << "Please Enter length:"; cin >> length; cout << "Please Enter height:"; cin >> height; } { float gallon; int area_length, area_width; int all_area; area_length = 2* area(length,height); all_area = area_length + area_width; gallon = all_area/250.00; } ประกาศฟังก์ชันชื่อ gallon มีการ return ค่าชนิด float และไม่มี parameter • เรียกฟังก์ชัน area • โดยส่ง parameter width และ height • ค่าที่ return กลับมาเก็บในตัวแปร area_width return ตัวแปร gallon

  7. ข้อ 4 • เขียนโปรแกรมเพื่อทําหน้าที่หาผลลัพธ์ของการคํานวณจากสมการ x = 2ab + c โดยเขียน • ฟังก์ชัน input() เพื่อรับข้อมูลเลขจํานวนเต็ม a b และ c • ฟังก์ชัน compute() เพื่อคำนวณผลลัพธ์จากสมการ และฟังก์ชันoutput() เพื่อแสดงผลลัพธ์ Lab6-4.cpp

  8. ข้อ 5 จงเขียนโปรแกรมเพื่อทำการหาค่าเลขยกกำลังโดยที่ให้กรอกเลขยกกำลังและกรอกเลขชี้กำลังโดยให้เขียนฟังก์ชันชื่อ • power(int x,int y) • Answer = xy • ให้รับค่า x และ y Lab6-5.cpp

More Related