วันจันทร์ที่ 26 สิงหาคม พ.ศ. 2556

Function

Easy

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 271

Question :  write the  function called power that it return input parameters 
แปล :  เขียนฟังก์ชั่น  ที่เรียกว่า power ให้ส่งค่ากลับ พารามิเตอร์ ที่นำเข้า 

code

int x=5;
int y=3;
void setup() {//function setup() จะมีการประกาศ กำหนด แต่ไม่ต้องเรียกใช้
  size(200,200);
  background(2);
  textSize(30);
  text("result="+x,40,100);
}
int power(int x, int y) {//function power เป็นfunction ชนิด int มีการรับ parameter และสามารถรีเทิร์นค่าได้
  return x ;
}




result





Medium

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 271
Question :  write the void function called Car that has three int parameters,num1,num2,and s. 
แปล :  เขียนฟังก์ชั่น void ที่เรียกว่า Car ที่มี 3  พารามิเตอร์ คือ num1,num2,and s .

code
void setup(){//function setup() จะมีการประกาศ กำหนด แต่ไม่ต้องเรียกใช้
  size(200,200);
  background(255);
}
  int num1=70;
  int num2=45;
  int s=50;
void draw(){//function void draw() ฟังก์ชั่นนี้ ก็มีการประกาศ กำหนด แต่ไม่ต้องเรียกใช้ เพราะมันจะเรียกใช้เองโดยอัตโนมัต
   drawCar(num1,num2,s);//เป็นส่วนที่เรียกใช้ void drawCar
}
void drawCar(int num1,int num2,int greatest){//เป็นการประกาศและกำหนด user defined function(ฟังก์ชั่นที่ผู้ใช้กำหนดขึ้นเอง) drawCar มีการรับค่า parameter
   rect(num1,num2,70,s);
   ellipse(num1+15,num2+50,s-30,s-30);
   ellipse(num1+55,num2+50,s-30,s-30);
}
result



Hard

Programming in C++ / Nell Dale,Chip Weems.
    ISBN : 0-7637-3234-6(pbk.)
    page : 272


Question :  write a void function ,TimeAdd,that takes parameter in days, hours,and minutes,and adds them to take a new time .Each part of the time is int ,and hours range from 0 to 23,while minutes range from  0 to 59.
and 0 to 30 on the range of days. 
แปล :  เขียนฟังก์ชั่น void, TimeAdd, ที่ใช้พารามิเตอร์ที่เป็น วัน, ชั่วโมง, และนาที โดยเพิ่มให้ใช้เวลาใหม่,แต่ละส่วนจะใช้ int และช่วงเวลา 0-23 ,ในขณะที่ช่วงนาทีคือ 0-59 ,และ 0-30 ในช่วงของวัน.

code

void setup() {{//function setup() จะมีการประกาศ กำหนด แต่ไม่ต้องเรียกใช้
  size(300,300);
  background(2);
  TimeAdd(5,6,12);//เป็นส่วนที่เรียกใช้ void TimeAdd มีการส่งค่า parameter ให้กับ  function
}
void TimeAdd(int days,int hours,int minutes){//เป็นการประกาศและกำหนด user defined function(ฟังก์ชั่นที่ผู้ใช้กำหนดขึ้นเอง) มีการรับค่า parameter จาก function setup
  if(days==0||days<=30){
    textSize(20);
    text("Day="+days,110,50);
  }else{
    textSize(20);
    text("No!",110,50);
  }
  if(hours==0||hours<=23){
    textSize(20);
    text("Hours="+hours,110,150);
  }else{
    textSize(20);
    text("No!",110,150);
  }
  if(minutes==0||minutes<=60){
    textSize(20);
    text("Minutes="+minutes,110,250);
  }else{
    textSize(20);
    text("No!",110,250);
  }
  
}

result


ไม่มีความคิดเห็น:

แสดงความคิดเห็น