Parameter Passing - C++ Functions TRB CSI › Programming with C++ › parameter passing

Parameter Passing - C++ Functions

Parameter passing is the mechanism used to transfer values or variables from a calling function to a called function. Tamil mixed: ஒரு function-இலிருந்து இன்னொரு function-க்கு values அல்லது variables-ஐ parameters மூலம் அனுப்புவது Parameter…

Premium — locked Advanced 24 min 161 cards 38 programs 8 MCQs v2
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

PARAMETER PASSING IN C++ ஒரு function-ஐ call செய்யும்போது, calling f……

Premium This card is part of the premium lesson. Unlock
Definition

Parameter passing is the mechanism used to transfer values or variables from a calling function to……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

add(10, 20); இங்கு: 10, 20 என்ற values a……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

இந்த program-ஐ பாருங்கள்: #incl……

Premium This card is part of the premium lesson. Unlock
Important
Important terminology

Parameter passing-ல் இரண்டு ம……

Premium This card is part of the premium lesson. Unlock
Important

1. Actual Parameter / Argumen……

Premium This card is part of the premium lesson. Unlock
Important

Function call செய்யும்போது கொடுக்கப்ப……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

display(a); இங்கு: a Actual p……

Premium This card is part of the premium lesson. Unlock

FORMAL PARAMETER

Explanation

Function definition-ல் value receive செய்ய பயன்படுத்தப்படும் var……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

add(a, b); } இங்கு: a, b → Actual Para……

Premium This card is part of the premium lesson. Unlock
Flow

main() | | a = 10 | b = 20 | | add(a,……

Premium This card is part of the premium lesson. Unlock
Explanation
Types of parameter passing in c++

C++-ல் முக்கியமாக மூன்று para……

Premium This card is part of the premium lesson. Unlock

Call by Value

Call by Reference

Call by Address / Pointer

Explanation

இதில் TRB, NET, SET, Intervie……

Premium This card is part of the premium lesson. Unlock
Comparison
vs

Call by Reference…

Premium This card is part of the premium lesson. Unlock

CALL BY VALUE

Definition

Function-க்கு original variable-ஐ அனு……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Call: change(a); a முழுமையாக function-……

Premium This card is part of the premium lesson. Unlock
Program
Program - call by value
Explanation

change(a);…

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Question

Function-ல்: x = 100; என்று மாற்றினோம். அப்……

Premium This card is part of the premium lesson. Unlock
Explanation
Line explanation

Initial: int a = 10; Memory concept: a +------+ | 10 | +------+ Function call: change(a); a value copy செய்யப……

Premium This card is part of the premium lesson. Unlock
Explanation
Key point

Call by Value: Change in formal parameter does not affect the act……

Premium This card is part of the premium lesson. Unlock
Explanation

change(a);…

Premium This card is part of the premium lesson. Unlock
Program
Step 1
Explanation

Step 2: change(a); Step 3: Va……

Premium This card is part of the premium lesson. Unlock
Program
Step 4
Program
Now
Explanation

Step 5: Function ends. x dest……

Premium This card is part of the premium lesson. Unlock

CALL BY REFERENCE

Definition

Original variable-க்கு ஒரு reference / alias function parameter ஆக……

Premium This card is part of the premium lesson. Unlock
Explanation

void functionName(int &x) { }…

Premium This card is part of the premium lesson. Unlock
Important

int &x இதன் பொருள்: x என்பது தன……

Premium This card is part of the premium lesson. Unlock
Program
Program - call by reference
Explanation

change(a);…

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Explanation
Why output 100?

Initial: a = 10 Function call: change(a); Function parameter: int &x x என்பது a-க்கு reference. Conceptua……

Premium This card is part of the premium lesson. Unlock
Output
Result
Explanation
Key point

In call by reference, changes made to the formal parameter affect the actual parameter.……

Premium This card is part of the premium lesson. Unlock
Important
Most important difference

Call by Value void test(int x) Call by Reference voi……

Premium This card is part of the premium lesson. Unlock
Comparison
Example comparison

Call by Value void test(int x) {……

Premium This card is part of the premium lesson. Unlock
Remember

VALUE = COPY REFERENCE = ORIGINAL அல்லது……

Premium This card is part of the premium lesson. Unlock

CALL BY ADDRESS / POINTER

Remember

C++-ல் address-ஐ pointer மூலம் function-க்கு pass ……

Premium This card is part of the premium lesson. Unlock
Explanation

Function: void change(int *x) { } Call: cha……

Premium This card is part of the premium lesson. Unlock
Explanation

change(&a);…

Premium This card is part of the premium lesson. Unlock
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock
Explanation
Line explanation

Variable: int a = 10; Suppose: a value = 10 a address = 1000 Call: change(&a); &a means: 1000 Address functio……

Premium This card is part of the premium lesson. Unlock
Explanation
& and * difference

Pointers புரிந்துகொள்ள இரண்டு symbols முக்கியம். & Address-of oper……

Premium This card is part of the premium lesson. Unlock
Flow
Pointer flow

a = 10 Address of a = 1000 | ↓ change(&a) | ……

Premium This card is part of the premium lesson. Unlock
Comparison
Three methods comparison
Real Life Example
Very important example - swapping

Parameter passing கேள்விகளில் மிகவும் common example: Swap two numbers WRONG SWAP USING CALL BY VALUE #i……

Premium This card is part of the premium lesson. Unlock

Swap function உள்ளே நடந்தாலும……

Premium This card is part of the premium lesson. Unlock

CORRECT SWAP USING CALL BY REFERENCE

Explanation
Why reference swap works?

Initially: a = 10 b = 20 References: x → a y → b Then: temp = x; temp =……

Premium This card is part of the premium lesson. Unlock
Explanation
Swap using pointer

#include using namespace std; void swapNumbers(int *x, int *y) { in……

Premium This card is part of the premium lesson. Unlock
Explanation
Pass by const reference

C++-ல் ஒரு large object-ஐ copy செய்யாமல் function-க்கு pass செய……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

இங்கு: unnecessary string copy இல்லை original ……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Example

#include #include using namesp……

Premium This card is part of the premium lesson. Unlock
Explanation
Why const reference?

Suppose: string text = "Very large data..."; Call by value: void show(string text) copy உருவாகலாம……

Premium This card is part of the premium lesson. Unlock
Explanation
Default parameters

Parameter passing topic-இல் related important concept: Def……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

return 0; }…

Premium This card is part of the premium lesson. Unlock

Call: display(); argument இல்……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Another default parameter example

void add(int a, int b = 5) { ……

Premium This card is part of the premium lesson. Unlock
Important
Important default argument rule

Default arguments generally rightmost parameters-லிருந்து கொடுக்க வேண்டும். Correct: void fun(int a, int b = ……

Premium This card is part of the premium lesson. Unlock
Explanation
Parameter count

Function declaration: void add(int a, int b) என்றால் normally two arguments ……

Premium This card is part of the premium lesson. Unlock
Explanation
Parameter order

Parameter order முக்கியம்.…

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Call: subtract(20, 5); Mappin……

Premium This card is part of the premium lesson. Unlock

But: subtract(5, 20);…

Premium This card is part of the premium lesson. Unlock

So arguments position-based m……

Premium This card is part of the premium lesson. Unlock
Explanation
Type matching

Formal parameter: void show(int x) Call: show……

Premium This card is part of the premium lesson. Unlock

10.8 double value int parameter-க்கு conversion ஆகலாம்.

Result may become: 10 அதனால் ……

Premium This card is part of the premium lesson. Unlock
Explanation
Value parameter lifetime

Call by value: void test(int x) { } x function enter ஆகும்போது உருவாகிறது. Function மு……

Premium This card is part of the premium lesson. Unlock
Important
Reference parameter important point

void change(int &x) இங்கு x new ……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

int a = 10; int &x = a;…

Premium This card is part of the premium lesson. Unlock
Concept

a and x ↓ same object So: x =……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 1

Call by value பயன்படுத்தி original value change ஆகும் என்று நினை……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 2

Reference syntax தவறு: Wrong: ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 3

Pointer function-ஐ wrong call செய்வது. Function: void c……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 4

Dereference செய்யாமல் pointer variable-க்கு value assign……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Common error 5

Reference parameter-க்கு call செய்யும்போது & எழுதுவது. Function: void change(int &x) Correct call: change……

Premium This card is part of the premium lesson. Unlock
Memory Trick

Reference → & in definition P……

Premium This card is part of the premium lesson. Unlock
Important

Call by Value void fun(int x) Copy. Call by Reference void f……

Premium This card is part of the premium lesson. Unlock
Explanation
When to use which?

Use Call by Value when: Original data change ஆகக்கூடாது Data is small Independent copy வேண்டும் int square(in……

Premium This card is part of the premium lesson. Unlock
MCQ

In which parameter passing method does ……

Premium This card is part of the premium lesson. Unlock
MCQ

Which symbol is used for refe……

Premium This card is part of the premium lesson. Unlock
MCQ

Consider: void fun(int x) { x……

Premium This card is part of the premium lesson. Unlock
TRB Point
Question 4

void fun(int &x) { x = 20; } ……

Premium This card is part of the premium lesson. Unlock
MCQ

Actual values supplied in a f……

Premium This card is part of the premium lesson. Unlock
MCQ

Parameters written in a funct……

Premium This card is part of the premium lesson. Unlock
MCQ

Which uses a copy of the actu……

Premium This card is part of the premium lesson. Unlock
MCQ

Which can directly modify the……

Premium This card is part of the premium lesson. Unlock
MCQ

Which declaration represents ……

Premium This card is part of the premium lesson. Unlock
Interview Point

Is C++ really "call by reference"? In practical C++ terminology: void fun(int &x) is called pass by ……

Premium This card is part of the premium lesson. Unlock
Interview Point

Why is pass by const referenc……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

void display(const string &s)……

Premium This card is part of the premium lesson. Unlock
List

1. Avoids copying large object 2. ……

Premium This card is part of the premium lesson. Unlock
Memory Trick

Remember this: int x ↓ VALUE ↓ COPY ↓ ORIGINAL SAFE int &x……

Premium This card is part of the premium lesson. Unlock
TRB Point
Exam short note

Parameter passing என்பது calling function-லிருந்து called function-க்கு arguments அனுப்பும் mechanism ஆகும். ……

Premium This card is part of the premium lesson. Unlock
Summary

Parameter Passing | ├── Actual Parameter | ↓ |……

Premium This card is part of the premium lesson. Unlock
Summary

1. Pass by Value ↓ Copy ↓ Original unchanged 2. Pass by Reference ↓ ……

Premium This card is part of the premium lesson. Unlock
Summary

One-line memory: Value = Copy……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program