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 @ 2022-10-21 12:16  铧轩  阅读(44)  评论(0编辑  收藏  举报