02 - Functions

functions

img
img
img

Thinking Recursively

Solving a problem with recursion requires two steps.

  • First, determine how to solve the problem for simple cases. This is called the base case.
  • Second, determine how to break down larger cases into smaller instances. This is called the recursive step.

Summing Up Digits

img

iteratively

int sumOfDigitsOf(int n){
int result = 0;
while (n > 0){
result += (n % 10);
n /= 10;
}
return result;
}
int main() {
int n = getInteger("Enter an integer: ");
int digitSum = sumOfDigitsOf(n);
cout << n << " sums to " << digitSum << endl;
return 0;
}

recursively

int sumOfDigitsOfByRecursion(int n){
if (n < 10){
return n;
} else {
return sumOfDigitsOfByRecursion(n / 10) + (n % 10);
}
}
int main() {
cout << "Summing up digits by recursion" << endl;
int n = getInteger("Enter an integer: ");
int digitSum = sumOfDigitsOfByRecursion(n);
cout << n << " sums to " << digitSum << endl;
return 0;
}

Factorials

  • The number n factorial, denoted n!, is n × (n – 1) × ... × 3 × 2 × 1
int factorial(int n){
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main(){
int num = getInteger("Enter a integer: ");
int factorialOfIt = factorial(num);
cout << "Factorail of " << n << " is: " << factorail << endl;
return 0;
}

Digital Roots

  • The digital root is the number you get by repeatedly summing the digits of a number until you’re down to a single digit.

Q: What is the digital root of 5?
A: 5 is a single digit, so the answer is 5.

Q: What is the digital root of 27?
A: 2 + 7 = 9.
The answer is 9.

Q: What is the digital root of 137?
A: 1 + 3 + 7 = 11.
1 + 1 = 2.
The answer is 2.

int digitalRoot(int n){
if(n < 10) {
return n;
}
else {
return digitalRoot(digitalRoot(n / 10) + (n % 10));
}
}
int main(){
int num = getInteger("Enter a integer: ");
int result = digitalRoot(num);
cout << "Digital root of " << n << " is: " << result << endl;
return 0;
}

Reverse a string

recurcively

string reverse(string s){
if (s.substr(1) == ""){
return s;
} else {
return reverse(s.substr(1)) + s[0];
}
}
int main(){
string s = getLine("Enter a string that you wanna reverse: ");
string reversed = reverse(s);
cout << "Reversed of " << s << " is: " << reversed << endl;
}

Parameter Passing in C++

  • General principle: When passing a string into a function, use pass-by-reference unless you actually want a copy of the string.

img

"Pass by value" (default behavior of parameters)

  • The function twiceOf takes the value of main’s variable a as input, but the change in twiceOf only happens to a local copy named a.
int twiceOf(int a){
return a *= 2;
}
int main(){
int num = 12;
cout << twiceOf(num) << endl;
return 0;
}
// the answer here is `12`

"Pass by reference" - &

  • I like to think of the & asa rope lasso that grabs the input parameter and drags it into the function call directly, rather than making a copy of its value and then leaving it in place.
int twiceOf(int& a){
return a *= 2;
}
int main(){
int a = 12;
cout << twiceOf(a) << endl;
return 0;
}
// the answer here is `24`

Pass-by-const-Reference

  • If you want to look at, but not modify, a function parameter, pass it by const reference: The “by reference” part avoids a copy.
  • The const (constant) part means that the function can’t change that argument.

For example:

void proofreadLongEssay(const string& essay) {
/* can read, but not change, the essay. */
}
posted @   铧轩  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示