以下是一個計算平方和立方的函數(shù)的示例代碼:
#include <iostream>
int square(int num) {
return num * num;
}
int cube(int num) {
return num * num * num;
}
int main() {
int num;
std::cout << "Enter a number: ";
std::cin >> num;
int squareResult = square(num);
int cubeResult = cube(num);
std::cout << "Square of " << num << " is: " << squareResult << std::endl;
std::cout << "Cube of " << num << " is: " << cubeResult << std::endl;
return 0;
}
在這個示例中,square
函數(shù)用于計算一個數(shù)的平方,cube
函數(shù)用于計算一個數(shù)的立方。在main
函數(shù)中,用戶輸入一個數(shù),然后調(diào)用square
和cube
函數(shù)分別計算平方和立方,并輸出結(jié)果。