Question

Explain different ways of calling functions with appropriate examples In C++

Explain different ways of calling functions
with appropriate examples
In C++

Homework Answers

Answer #1

There are TWO ways of calling functions:

1) Call by Value: In this technique we pass the Value to arguments which are copied into formal parameters of the functions. Hence, the original values are unchanged only parameters inside function changes.

void Add(int x)
{
  x = x + 10;
}

int main()
{
  int x = 10;
  Add(x);

  // AS YOU CAN SEE VALUE Of X REMAINS 10
  cout << "x: " << x;
  // OUTPUT- x: 10
}

2) Call by Reference: In this we pass the address of the variable as arguments. In this case the formal parameter can be taken as a reference or a pointer, in both case they will change the values of the original value.

void Add(int &x)
{
  x = x + 10;
}

int main()
{
  int x = 10;
  Add(x);
  
  // NOW YOU CAN SEE VALUE OF X CHANGED TO 20
  cout << "x: " << x;
  // OUTPUT- x: 20
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using examples from insects and crustaceans, explain, in addition, to biodiversity, THREE (3) different ways in...
Using examples from insects and crustaceans, explain, in addition, to biodiversity, THREE (3) different ways in which arthropods are regarded as a highly successful phylum?
Please explain the functions of cofactors with examples.
Please explain the functions of cofactors with examples.
What are the different ways to obtain audit evidence? Can you give examples of each? What...
What are the different ways to obtain audit evidence? Can you give examples of each? What does it mean for audit evidence to be sufficient and appropriate, and why is it necessary?
Give examples of the different ways that a client’s family and carer can be included as...
Give examples of the different ways that a client’s family and carer can be included as part of the support team.
What is CIA? Explain it with appropriate examples?
What is CIA? Explain it with appropriate examples?
Using specific examples, describe three different ways in which endocrine glands can be stimulated to release...
Using specific examples, describe three different ways in which endocrine glands can be stimulated to release their products.
1. Give two heuristic functions for the 8-puzzle. 2. Explain minimax search. Use examples. 3. Explain...
1. Give two heuristic functions for the 8-puzzle. 2. Explain minimax search. Use examples. 3. Explain alpha-beta pruning. Use examples.
Using examples, explain the three (3) main ways to applying styles to an HTML document.
Using examples, explain the three (3) main ways to applying styles to an HTML document.
Explain with examples the Social, Legal, and political functions of local government units in a State....
Explain with examples the Social, Legal, and political functions of local government units in a State. Please help me out. I need much explanations
Explain the different functions of inventory, and identify the types and functional use.
Explain the different functions of inventory, and identify the types and functional use.