Kyurious Minds Computer Academy C++ Chapter 4: Functions

Chapter 4: Functions


Key Points:

  • Understand function declaration, definition, and prototypes.
  • Learn how to pass parameters, return values, and use default arguments.
  • Explore variable scope and lifetime including local, global, and static variables.
  • Implement function overloading and recursion effectively.

1. Function Declaration and Definition

A. Function Declaration

  • Syntax:
  return_type function_name(parameter_list);
  • Example:
  int add(int, int);

B. Function Definition

  • Syntax:
  return_type function_name(parameter_list) {
      // code to be executed
      return value;
  }
  • Example:
  int add(int a, int b) {
      return a + b;
  }

C. Function Prototypes

  • Purpose: Declare a function before its actual definition to inform the compiler about its existence.
  • Example:
  double calculateArea(double radius); // Prototype
  double calculateArea(double radius) { // Definition
      return 3.14159 * radius * radius;
  }

2. Function Parameters and Return Values

A. Passing Arguments to Functions

  • By Value: A copy of the argument is passed.
  • Example:
    cpp void printValue(int value) { cout << value << endl; }

B. Returning Values from Functions

  • Syntax:
  return_type function_name(parameters) {
      // code
      return value;
  }
  • Example:
  int multiply(int x, int y) {
      return x * y;
  }

C. Default Arguments

  • Syntax: Provide default values for parameters.
  • Example:
    cpp void greet(string name = "Guest") { cout << "Hello, " << name << "!" << endl; }

D. Multiple Return Values

  • Approach: Use std::pair or std::tuple from the STL to return multiple values.
  • Example:
    cpp #include <utility> // For std::pair std::pair<int, int> getMinMax(int a, int b) { if (a < b) return std::make_pair(a, b); else return std::make_pair(b, a); }

3. Scope and Lifetime of Variables

A. Local Variables

  • Definition: Variables declared within a function or block. They are accessible only within that function or block.
  • Example:
  void someFunction() {
      int localVar = 10; // local to someFunction
  }

B. Global Variables

  • Definition: Variables declared outside all functions. They are accessible from any function within the same file.
  • Example:
  int globalVar = 20; // global variable
  void anotherFunction() {
      cout << globalVar << endl; // Access globalVar
  }

C. Static Variables

  • Definition: Variables that retain their value between function calls. Declared using the static keyword.
  • Example:
  void countCalls() {
      static int callCount = 0;
      callCount++;
      cout << "Function called " << callCount << " times." << endl;
  }

D. Function Overloading

  • Definition: Multiple functions with the same name but different parameter lists.
  • Example:
  int add(int a, int b) {
      return a + b;
  }
  double add(double a, double b) {
      return a + b;
  }

4. Recursion

A. Understanding Recursion

  • Definition: A function that calls itself to solve smaller instances of the same problem.
  • Example:
  int factorial(int n) {
      if (n <= 1) return 1;
      return n * factorial(n - 1);
  }

B. Base Case and Recursive Case

  • Base Case: Condition to terminate the recursion.
  • Recursive Case: Part of the function where the recursion occurs.
  • Example:
  void printNumbers(int n) {
      if (n <= 0) return; // Base case
      printNumbers(n - 1); // Recursive case
      cout << n << " ";
  }

C. Recursive Functions Examples

  • Factorial:
  int factorial(int n) {
      if (n <= 1) return 1;
      return n * factorial(n - 1);
  }
  • Fibonacci Sequence:
  int fibonacci(int n) {
      if (n <= 1) return n;
      return fibonacci(n - 1) + fibonacci(n - 2);
  }

Practice Tasks:

  • Write a function that calculates the area of a rectangle and returns the result.
  • Implement a recursive function to compute the nth Fibonacci number.
  • Create overloaded functions for different mathematical operations.