Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners turbo C Question 6.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Arijit Maji
turbo C Question 6.
2 Answer(s)      2 years and 10 months ago
Posted in : Java Beginners


Explain call by value and call by reference with easy examples.


Advertisement
View Answers

February 28, 2012 at 10:55 AM


Call by value: In call by value method, the called function creates a new set of variables and copies the values of arguments into them. Example: Program showing Call by value method

void swap(int x, int y)
{ 
  int temp;
  temp = x;
  x = y;
  y = temp;
  printf("Swapped values are a = %d and b = %d", x, y);
}

void main()
{
  int a = 7, b = 4;
  swap(a, b);
  printf("Original values are a = %d and b = %d", a, b);
  printf("The values after swap are a = %d and b = %d", a, b);
}

Output: Original Values are a = 7 and b = 4 Swapped values are a = 4 and b = 7

The values after swap are a = 7 and b = 4 This happens because when function swap() is invoked, the values of a and b gets copied on to x and y. The function actually swaps x and y while the original variables a and b remains intact.

Call by reference: In call by reference method, instead of passing a value to the function being called a reference/pointer to the original variable is passed. Example: Program showing Call by reference method

void swap(int *x, int *y)
{
  int temp;
  temp = *x;
  *x = *y;
  *y = temp;
  printf("Swapped values are a = %d and b = %d", *x, *y);
}
void main()
{
  int a = 7, b = 4;
  swap(&a, &b);
  printf("Original values are a = %d and b = %d",a,b);
  printf("The values after swap are a = %d and b = %d",a,b);
}

Output: Original Values are a = 7 and b = 4 Swapped values are a = 4 and b = 7

The values after swap are a = 4 and b = 7 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.



February 28, 2012 at 10:57 AM


For Java

Visit Here



Related Tutorials/Questions & Answers:
turbo C Question 6.
turbo C Question 6.  Explain call by value and call by reference with easy examples
turbo C Question 4.
turbo C Question 4.  Possible to print the following (Using for Loops): 1. * ** 2.* * ** * * * *   specify the format properly
Advertisements
turbo C Question 4.
turbo C Question 4.  Possible to print the following (Using for Loops): 1. * ** 2.* * ** * * * *   specify the format properly
turbo C Question 5.
turbo C Question 5.  is it possible to print "", 1st line (5 times),2nd line *(4 times),3rd line *(3 times),4th line *(2 times) & 5th line *(1 times) with Align Right. and also reverse order as 1,2,3,4,5 (but right aline
turbo c pre-increment question
turbo c pre-increment question  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times
turbo C prog.
turbo C prog.  Is is possible print ASCII value of a alphabets using turbo C prog
turbo C prog.
turbo C prog.  Is it possible to print ASCII value A to Z with out...;#include<stdio.h> #include<conio.h> void main() { for(char c='A';c<='Z';c++){ printf("\nASCII of %c is %i\n", a, a); } getch(); } C
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo C  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)  Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print
turbo C prog. Q3
turbo C prog. Q3  What is the difference between if and ladder... } Example of ladder if: #include <stdio.h> main() { int a,b,c; printf("enter values for a,b,c"); scanf("%d%d%d",&a,&b,&c); if(a>
Conditional Statement in Turbo C
Conditional Statement in Turbo C   Write a program that computes and assesses the tuition fee of the students in one trimester, based on the given mode of payment below: Plan (Key) Discount (-) or Interest (+) Cash (1) 10
Conditional Statement in Turbo C
Conditional Statement in Turbo C   Write a program that computes and assesses the tuition fee of the students in one trimester, based on the given mode of payment below: Plan (Key) Discount (-) or Interest (+) Cash (1) 10
(ForPro) turbo C prog. Q4
(ForPro) turbo C prog. Q4  I need to learn some easy foxpro program.. for example sum,interchange, loop etc
how to get following answer in turbo c program
how to get following answer in turbo c program  input 123456789 output 1 2 3 4 5 6 7 8 9
C# question
Create a Solution by name Bank with Classes Account.cs, SavingsAccount.cs and savingsTest.cs to Demonstrate Abstract Classes in C#  1st question... and savingsTest.cs to Demonstrate Abstract Classes in C
question for c++
question for c++   write Program to swap between two numbers if first number is greater than second number and total of two numbers greater than 10
C# question
C# question  Explain enumeration base types in C
C# question
C# question  In C#, string is a class or primitive type. Explain
c question
c question  How to convert decimal numbers into binary digits without using pow function in c language
C# question
C# question  Write a program in C# to create 3*4 matrix and show the sum of diagonal values
C# question
C# question  How can we call method pass by reference in C
C# question
C# question  Write a program in C# to create 3*4 matrix and show the sum of diagonal values
question for c++
question for c++   write Program to swap between two numbers if first number is greater than second number and total of two numbers greater than 10   ?¨?±???§???¬ ??????¨?§?¯????© ?¨
C/C++/JAVA Question on function
C/C++/JAVA Question on function  "Write a function to find out common... other data type also.â?? Question can be solved in C,C++ or JAVA. (Recommneded - C
C++ question 3
C++ question - add the two matrices and display the elements of resultant....   C sum of two matrices The given code allow the user to enter...; #include <conio.h> void main() { int m, n, c, d, first[10][10], second[10
C++ Question 2
C++ Triangle and Rectangle - calculate area of Triangle and rectangle using data members from base class  2.create class. Shape accepts two values (Data Type: Double). Create two derived classes Triangle and Rectangle ; calculate
C Program Question - Development process
C Program Question  How i can calulate sum of First digit and last digit of Five Numbers. e.g 12345 i want to calculate 1+5=6 how i can calculate in C Program?   Hi Friend, Try the following code
c++
c++  i use turbo c++...i want to change the background color...what is the command for it and the header file used
c++
c++  i use turbo c++...i want to change the background color...what is the command for it and the header file used
c++
c++  i use turbo c++...i want to change the background color...what is the command for it and the header file used
C/C++ Programming Books
;   Visual C++ 6 Unleashed Visual C++ 6 Unleashed provides comprehensive coverage of the core topics for Visual C++ 6 programming. This book skips the beginning level material and jumps
 

 

 

DMCA.com