CPlusOne
Notes, Syllabus, Questions, Ideas,
Books, Reference materials on Electronics


Quick Links
#Login #Sign up #Forum #Feedback

 

#Previous question #Index of C Questions #Next question

Call by value: In the call by value method, the called function creates a new set of variables in stack and copies the values of the arguments into them.
Example: Program showing the Call by Value method
void swap(int x, int y)
{
int z;
z = x;
x = y;
y = z;
printf("Swapped values are a = %d and b = %d", x, y);
}
10 
11  void main()
12  {
13  int a = 7, b = 4;
14  printf("Original values are a = %d and b = %d", a, b);
15  swap(a, b);
16  printf("The values after swap are a = %d and b = %d", a, b);
17  }
18 
19  Output:
20  Original Values are a = 7 and b = 4
21  Swapped values are a = 4 and b = 7
22  The values after swap are a = 7 and b = 4
23 
This happens because when function swap() is invoked, the values of a and b gets copied onto x and y. The function actually swaps x and y while the values of the original variables a and b remain intact.

Here is the block diagram describing disassembly steps, call stack and argument variables. Calling swap (a, b) can be split into some assembly steps like-

  1. push value of b
  2. push value of a
  3. save return address
  4. call function

Call by value - describing disassembly steps, call stack and argument variables

Here one point to note is x and y are in stack. Value of a and b will be copied to x and y. Inside swap() value of x and y will be interchanged but it will not affect a and b in the main().

Call by reference: In the call by reference method, instead of passing values to the function being called, references/pointers to the original variables are passed.
Example: Program showing the Call by Reference method
void swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
printf("Swapped values are a = %d and b = %d", *x, *y);
}
10  void main()
11  {
12  int a = 7, b = 4;
13  printf("Original values are a = %d and b = %d", a, b);
14  swap(&a, &b);
15  printf("The values after swap are a = %d and b = %d", a, b);
16  }
17 
18  Output:
19  Original Values are a = 7 and b = 4
20  Swapped values are a = 4 and b = 7
21  The values after swap are a = 4 and b = 7
22 
This happens because when function swap() is invoked, it creates a reference for the first incoming integer a in x and the second incoming integer b in y. Hence the values of the original variables are swapped.

Here is the block diagram describing disassembly steps, call stack and argument variables. Calling swap (&a, &b) can be split into some assembly steps like-

  1. push address of b
  2. push address of a
  3. save return address
  4. call function

Call by reference - describing disassembly steps, call stack and argument variables

Here one point to note is x and y are pointers. Address of a and b will be copied to x and y. Inside swap() value of *x and *y will be interchanged which is same as changing a and b in the main().

You have viewed 1 page out of 204. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

structured programming language, first C program, header files, library, compilation, pre-processing, compilation, optimization, linking format, debugging with GDB, data types, storage classes, if, if-else, if-else-if, label and goto, switch case, loop statements, break and continue, pointers, enum type, macro, basic operators, logical operators, logical vs bitwise operators, bit shifting, bit set, reset/clear, toggle, Operator precedence, array, multidimensional arrays, 2D and 3D dynamic array, add Matrix, multiply Matrix, adjacency Matrix, Circuler buffer, user defined types, typedef, struct type, union type, struct vs union, struct pack padding, bit fields, obtain bit mask, reverse bits, swap bits, structure as function argument, array as function argument, printing pointers, scanf string input, scan string with blanks, scanf string ends with newline, return of scanf, return of printf, scanf with printf, print using fprintf, take input using fscanf, floating point formatting, multiple arguments in printf and scanf, variadic functions, variadic macro, calling conventions, calling convention of C,C++ and PASCAL, operator precedence, ternary operator, switch statement, continue in switch and loops, for, while, do-while loop syntax, for to while, do-while and while usage, infinite loops, function declaration and definition, library function linking, call-by-value and call-by-reference, parameter passing call-by-value, parameter passing call-by-reference, address of local and dynamic variable, volatile variable, external vs static, global vs static variable, register vs auto variable, const, macro vs constant, debug builds, debug macros, compiler macro for C++, FILE, LINE, DATE, TIME, compiler marcos, token pasting, characterizing, const char *,char * const, const char * const, char array and char pointers, enum vs macro, macro vs typedef, size of a structure, upper and lower 16bit of 32bit unsigned, near, far and huge pointer, typed pointers and void pointer, sizeof void, sizeof void and void pointer, operators for void pointer, l-value, struct and array, NULL pointer, inline-functions, macro vs inline-function, malloc vs calloc, function pointer, compare with ==, argc and argv, C startup routine, strcpy and strcat source, memcpy vs memmove, strrev source, strdup, strtok, palindrome, reverse decimal digits, signed and unsigned compare, string vs integer pointer, xor operator, bitwise shifting, logical and bitwise or, macro definition, long pointer increment, default C++ argument value, short pointer increment, short pointer increment, execution sequence, exceptions, setjump and longjump, trace a memory leak, detect memory corruption, Big Endian and Little Endian, stack grows up or down, prime number, HCF, LCM, star triangle, Exchange variables, C and Data Structures, Data Structures using array, Linked List, Single Linked list, Doubly Linked list, Circular Linked list, Doubly Circular Linked list, Queue, reverse single linked list, delete single linked list, Stack, Tree and Binary tree, Binary tree, traverse Binary tree, Binary search tree, double Binary tree, mirror Binary tree, height of Binary tree, heap, Complexity, Linear Search, Binary Search, Hash Table, Sorting, Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Heap Sort, Merge sort, Radix Sort, Recursion, factorial using recursion, Fibonaci using recursion, amrongstrong number, argv environ, system(), atexit, raise signal, abort exit, Libraries, static linking, dynamic linking methods, implicit dynamic linking, explicit dynamic linking, access C from VB, access C from Java, sleep/delay, sound, nosound, alpha numeric, convert case, string to int, int to string, fprintf, fscanf working principal, palindrome digits, arithmeic math, trigonometric, fopen, fdopen, write vs append mode, binary vs text mode, binary mode, fseek, fprintf, fscanf, fflush, ioctl, mmap, fcntl, poll, select, poll, select, fputs,fgets,fgetc,fputc, printf, scanf, DOS graphics, compilers, Turbo C, GCC, VC++,

# C/C++ Interview questions and answers - Shetty's World
# C Interview Questions and Answers - C Interview FAQs & C Books
# C interview questions and answers | TechInterviews
# C Interview Questions and Answers :: ALL Interview .com
# C Interview Questions and Answers

* #1 webmaster Sat 01 Feb/2014 09:35:18(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.