เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

First function

The first function you'll write in C++ may very well be the only function you will ever need as you will write a function that returns the answer to the ultimate question of life, the universe and everything.

Rcpp is loaded in your workspace.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Optimizing R Code with Rcpp

ดูคอร์ส

คำแนะนำการฝึกหัด

Write a C++ function the_answer() that does not take any argument and always returns the int 42.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Define the function the_answer()
cppFunction('
  ___ ___() {
    return ___ ;
  }
')

# Check the_answer() returns the integer 42
the_answer() == 42L
แก้ไขและรันโค้ด