Video summary
Lecture 5: Functions | DSA Series by Shradha Khapra Ma'am | C++
Main summary
Key takeaways
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:
intreturns an integer voidreturns nothing
- Example:
- Function name (e.g.,
mainor a custom name likeprintHello) - 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)
- stored in a variable (e.g.,
- Key rule:
returnshould be the last statement executed in that function.- Any code after
returnwill not execute.
- Any code after
- For
voidfunctions, usereturn;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)→10goes toa,5goes tob
- Function parameters:
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
- if yes, choose a correct return type (e.g.,
- Write the function header:
returnType functionName(parameters)
- Implement logic inside
{ ... }. - Use
return:- return the computed result if the return type is non-
void - ensure
returnis the final action
- return the computed result if the return type is non-
- 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 % 10digitSum += 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
ifrom1ton sum += i
- Initialize
- 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
1throughn
- multiply all integers from
- Loop approach:
- Initialize
factorial = 1 - Loop
ifrom1ton factorial *= i
- Initialize
- 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 = 5inmain, and a function modifies its parameterxto10,- then
mainstill hasx = 5after the function returns.
- then
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:
voidreturn type when only printing- invocation syntax
2) Sum of two numbers
- Parameterized function:
- inputs:
a,b - output:
a + b
- inputs:
3) Minimum of two numbers
- Return the smaller of
aandbusing comparison logic.
4) Sum from 1 to n
- A function that sums
1..nusing a loop. - Demonstrates why parameterizing with
navoids rewriting code.
5) Factorial of n
- Implements
n!using a loop and an accumulator initialized to1.
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
- the program starts at
7) Sum of digits
- Uses:
% 10and/ 10in a while loop to extract and sum digits
8) Binomial coefficient (nCr)
- Uses the factorial formula:
nCr = n! / (r! * (n-r)!)
- Demonstrates function composition:
nCrreuses the factorial function by calling it forn,r, andn-r
Homework assigned (as stated)
- Write a function to check if a number is prime or not.
- Write a function to print all prime numbers from 2 to n (note: 1 is not prime).
- 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.
- 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)