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 કરો.
  • યાદ રાખવાની યુક્તિ: 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