Video summary

Lecture 5: Functions | DSA Series by Shradha Khapra Ma'am | C++

Main summary

Key takeaways

Educational

Main ideas and concepts covered (Functions in C++ / DSA Series)

1) What a function is and why it exists

  • Functions are a core concept in programming because they allow you to perform a task repeatedly without rewriting the same code.
  • A function generally:
    • takes input
    • does work
    • returns output (or performs work only if it returns nothing).

2) Function structure (definition format)

A function is described as having:

  • Return type (what the function produces)
    • Example: int returns an integer
    • void returns nothing
  • Function name (e.g., main or a custom name like printHello)
  • Parameter list inside parentheses (...) (optional)
  • Body inside curly braces { ... }
    • the code inside the body runs when the function is called

3) Function definition vs function call

  • Defining a function alone does not run it.
  • A function runs only when it is invoked/called.
  • Typical call pattern: functionName(...); (uses parentheses for arguments and usually ends with a semicolon.)

4) Return values and using return

  • Functions can return values, which can be:
    • stored in a variable (e.g., int v = functionCall();)
    • used directly in expressions (e.g., printing the returned value)
  • Key rule: return should be the last statement executed in that function.
    • Any code after return will not execute.
  • For void functions, use return; to return control to the caller without returning a value.

5) Parameters and arguments (terminology)

  • Parameters: the variables listed in the function definition (inputs the function accepts).
  • Arguments: the actual values passed when calling the function.
  • Example concept:
    • Function parameters: int a, int b
    • Call: sum(10, 5) → 10 goes to a, 5 goes to b

6) Redundancy and code reuse

  • Repeating the same logic (even 5–10 lines) leads to redundancy (unnecessary repetition).
  • Best practice: convert repeated logic into functions.

Methodologies / instruction-like parts (detailed bullet lists)

A) How to create and use a simple function

  • Decide what the function should do (e.g., print “Hello”, compute sum, etc.).
  • Decide whether it needs to return a value:
    • if yes, choose a correct return type (e.g., int, double)
    • if no, use void
  • Write the function header:
    • returnType functionName(parameters)
  • Implement logic inside { ... }.
  • Use return:
    • return the computed result if the return type is non-void
    • ensure return is the final action
  • Call/invoke the function when you want it to run:
    • use its name and parentheses
    • if it returns a value, store it or use it directly

B) Computing sum of digits (digit-sum methodology)

Approach uses:

  • Modulo to extract the last digit
  • Division to remove the last digit

Algorithm steps:

  • Initialize digitSum = 0
  • While number > 0:
    • lastDigit = number % 10
    • digitSum += lastDigit
    • remove last digit: number = number / 10
  • Return digitSum

C) Computing sum from 1 to n (sum(1..n) methodology)

  • Use a loop with a running accumulator:
    • Initialize sum = 0
    • Loop i from 1 to n
    • sum += i
  • Return the sum (or print it depending on how the function is designed).

D) Computing factorial of n (factorial methodology)

  • Factorial definition:
    • multiply all integers from 1 through n
  • Loop approach:
    • Initialize factorial = 1
    • Loop i from 1 to n
    • factorial *= i
  • Return factorial

E) Passing arguments: “pass by value” concept

  • For typical primitive data types:
    • the function receives a copy of the argument
  • Consequence:
    • changes inside the function do not affect the original variables in the caller

Example described:

  • If x = 5 in main, and a function modifies its parameter x to 10,
    • then main still has x = 5 after the function returns.

It also briefly contrasts:

  • Pass by reference exists (to be taught later), allowing changes to reflect in the caller.

Example problems solved / demonstrated in the lecture

1) Example function: printing “Hello World”

  • Shows:
    • void return type when only printing
    • invocation syntax

2) Sum of two numbers

  • Parameterized function:
    • inputs: a, b
    • output: a + b

3) Minimum of two numbers

  • Return the smaller of a and b using comparison logic.

4) Sum from 1 to n

  • A function that sums 1..n using a loop.
  • Demonstrates why parameterizing with n avoids rewriting code.

5) Factorial of n

  • Implements n! using a loop and an accumulator initialized to 1.

6) Memory model: functions and stack frames

  • Key points:
    • the program starts at main (called automatically by the compiler)
    • each function call creates a stack frame
    • local variables and relevant logic live in that stack frame
    • when a function returns:
      • its stack frame is removed
      • control returns to the caller
      • local variables become inaccessible

7) Sum of digits

  • Uses:
    • % 10 and / 10 in a while loop to extract and sum digits

8) Binomial coefficient (nCr)

  • Uses the factorial formula:
    • nCr = n! / (r! * (n-r)!)
  • Demonstrates function composition:
    • nCr reuses the factorial function by calling it for n, r, and n-r

Homework assigned (as stated)

  1. Write a function to check if a number is prime or not.
  2. Write a function to print all prime numbers from 2 to n (note: 1 is not prime).
  3. Write a function to print the Fibonacci series term for a given n:
    • 0, 1, 1, 2, 3, 5, 8, 13, ...
    • each term is the sum of the previous two.
  4. Optional self-study: switch statement
    • mentioned as a simple conditional structure, not covered in the course (focus is on placement-oriented DSA).

Speakers / sources featured

  • Shradha Khapra Ma’am (mentioned in the video title; instructor referenced throughout)

Original video