Passing arguments: pass by value, pass by reference

Pass by value function को एक PHOTOCOPY थमाता है (original सुरक्षित और अपरिवर्तनीय); pass by reference उसे ADDRESS थमाता है, जिससे वह caller का variable बदल सकता है, और इसी तरह swap() आख़िरकार काम करता है।

11 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

जो swap, swap करने से इनकार करता है

दो toppers के marks गलत क्रम में दर्ज हो गए, और program को x और y swap करने हैं। आसान है, आप सोचते हैं, और लिखते हैं:

void swap(int a, int b) {

int t = a; a = b; b = t;

}

swap(x, y) call कीजिए, x और y print कीजिए... वैसे के वैसे। Function चला, logic बिलकुल सही है, और कुछ नहीं हुआ।

C की सबसे शिक्षाप्रद नाकामी। यह क्यों नाकाम होती है, और एक-अक्षर का इलाज, यही पूरा lesson है।

Theory

Photocopy बनाम घर की चाबी

किसी contractor को अपने घर के plan की photocopy थमाइए और वह उस पर कुछ भी लिख-पोत सकता है, आपका घर अछूता रहता है। यही pass by value है: functions को copies मिलती हैं। उसे अपना पता और चाबियाँ दे दीजिए, और उसके बदलाव असली कमरों पर होते हैं। यही pass by reference है: functions को address मिलता है, और उसके साथ, बदलने की ताक़त। नाकाम swap ने photocopies पर लिखा-पोता।

Theory

दोनों modes, औपचारिक रूप से

Pass by value (C का default): parameters, argument values की ताज़ा LOCAL copies होती हैं। swap के a और b, x और y की copies थे; copies swap हुईं, फिर return पर मर गईं (पिछले lesson का फाड़ा गया drawer)। x और y को कभी पता ही नहीं चला।

Pass by reference (pointers के ज़रिए): ADDRESSES pass कीजिए, उन्हें pointer parameters में लीजिए, dereference के ज़रिए बदलिए:

void swap(int *a, int *b) {

int t = *a; *a = *b; *b = t;

}

swap(&x, &y) के रूप में call किया जाता है। अब *a ही x है (दो रास्ते, एक box, pointers वाला lesson), इसलिए swap असली चीज़ पर होता है।

Practical

नाकाम और कामयाब swap, साथ-साथ

#include <stdio.h>

void swapCopy(int a, int b)   { int t = a; a = b; b = t; }
void swapReal(int *a, int *b) { int t = *a; *a = *b; *b = t; }

int main() {
    int x = 90, y = 55;

    swapCopy(x, y);
    printf("After swapCopy: x=%d y=%d\n", x, y);

    swapReal(&x, &y);
    printf("After swapReal: x=%d y=%d\n", x, y);
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

दोनों lines का अनुमान लगाइए

Code trace कीजिए: swapCopy को values 90 और 55 मिलती हैं; swapReal को ADDRESSES मिलते हैं। दोनों printf lines क्या दिखाती हैं?

Show the answer

After swapCopy: x=90 y=55 (copies swap हुईं और मर गईं; originals अछूते)

After swapReal: x=55 y=90 (dereference वाला swap असली boxes तक पहुँचा)

एक ही logic, एक फ़र्क़: function में क्या गया, values या addresses। यही दो-line वाला trace हर साल argument passing का exam सवाल है।

Quiz

scanf("%d", &marks) & की माँग क्यों करता है जबकि printf("%d", marks) नहीं?

  1. scanf को marks में लिखना है, इसलिए उसे address चाहिए; printf सिर्फ value की copy पढ़ता है
  2. scanf पुराना syntax है, printf नया
  3. printf चुपके से खुद & जोड़ लेता है
  4. यह C की एक बेतुकी असंगति है
Show the answer

scanf को marks में लिखना है, इसलिए उसे address चाहिए; printf सिर्फ value की copy पढ़ता है

printf एक value इस्तेमाल करता है: एक photocopy काफी है। scanf का पूरा काम आपके variable में input पहुँचाना है, output-parameter वाला काम, इसलिए उसे चाबी (address) चाहिए। पूरा घेरा: Unit 1 का रहस्यमय &, pointers unit की व्याख्या, और आज की शब्दावली, printf value से pass करता है, scanf को reference चाहिए।

Quiz

एक function को array मिलता है: void curve(int marks[], int n), और हर element में 5 जोड़ता है। Call के बाद, caller का array...

  1. बदल जाता है: array अपने पहले element के ADDRESS के रूप में pass होता है
  2. वैसा का वैसा: arrays, ints की तरह copy होते हैं
  3. तभी बदलता है जब function उसे return करे
  4. मिट जाता है: arrays functions में नहीं जा सकते
Show the answer

बदल जाता है: array अपने पहले element के ADDRESS के रूप में pass होता है

Array का NAME एक address है (signpost वाला lesson), इसलिए function में जो जाता है वह caller के असली boxes का pointer है: बदलाव originals पर होते हैं, कोई & नहीं चाहिए। Arrays असल में हमेशा pass-by-reference होते हैं, और 60 boxes copy करना वैसे भी फ़िज़ूलख़र्ची होती। इसीलिए lab exams में grace-marks वाले functions बस काम कर जाते हैं।

Watch out

Marks कहाँ कटते हैं

नाकाम swap को जवाब के रूप में लिखना: values अंदर swap होती हैं, originals अछूते, दोनों versions पक्के याद रखिए। Call पर & भूलना: swapReal(x, y), जहाँ addresses चाहिए वहाँ VALUES थमाता है, compilers ज़ोर से warn करते हैं। Toppers के लिए ईमानदार footnote: तकनीकी रूप से C हमेशा pass-by-value है, pointer एक address है जो value से pass होता है; "pass by reference" pointer technique का चालू नाम है। यह बारीकी लिखने पर इज़्ज़त मिलती है, न लिखने पर BCA स्तर पर कुछ नहीं जाता।

Theory

कब कौन सा इस्तेमाल करें

Function सिर्फ जो inputs पढ़ता है: by value (सुरक्षा: caller का data नुक़सान नहीं पा सकता)। Outputs, कई results (पिछले lesson का total-percentage-grade वाला मसला), या बड़े arrays: by reference। एक value बाहर = return; कई values बाहर = addresses अंदर। C में दो lessons बाकी हैं: एक function जो खुद को call करता है, और वह struct जो आख़िरकार हमारे student को एक single record देता है।

Summary

Key takeaways

  • Pass by value (default): parameters copies हैं; caller के variables सुरक्षित और अपरिवर्तनीय।
  • Pass by reference (pointers): &x को int a में pass कीजिए; a असली variable बदलता है।
  • क्लासिक सबूत: swap(int,int) नाकाम; swap(int,int), &x, &y के साथ कामयाब।
  • scanf को & चाहिए (address से लिखता है); printf value की copy पढ़ता है।
  • Arrays, element zero के address के रूप में pass होते हैं: functions original बदलते हैं।
  • एक result = return; कई results = addresses pass कीजिए।
  • याद रखने का hook: photocopy बनाम घर की चाबी।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Passing arguments: pass by value, pass by reference · Computer Programming and Programming Methodology (CPPM) · Gri-Learn