How are function declarations function definitions and funct
Solution
a-
Function declaration, is done to tell the compiler about the existence of the function. Function\'s return type, its name & parameter list is mentioned. Function body is written in its definition. Lets understand this with help of an example.
#include < iostream>
using namespace std;
int sum (int x, int y); //declaring function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling function
cout << c;
}
int sum (int x, int y) //defining function
{
return (X + y);
}
Here, initially the function is declared, without body. Then inside main() function it is called, as the function returns sumation of two values, hence z is their to store the value of sum. Then, at last, function is defined, where the body of function is mentioned. We can also, declare & define the function together, but then it should be done before it is called.
b-
In C, the parameters which you are passing to a function are called Actual parameters and the parameters which are received by the function are called Formal parameters. Whenever a function is called a chunk of memory is allocated for the function.
So here comes my answer to your question-
Pass by reference:- In this, address of variable is passed to the function. Whatever changes we make to the formal parameter will affect the actual parameter as a same memory is allocated for both of them. We generally use pointers for this.
Pass by value:- Value of variable is passed. Any changes made to the formal variable will not affect the actual parameter as different memories are allocated for both of them. A temp variable is created in the function stack which does not affect the original variable.
Call by Value
Call by Reference
In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.
In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.
In call by value, actual arguments will remain safe, they cannot be modified accidentally.
In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results.
c-
Purpose of pre-condition and post-condition-
Pre-Condition :
A condition that should return true when a member function is invoked. In order to use a function correctly a precondition should return true. If a precondition fails to hold, an operation will not take responsibility to perform any action of sensibility. For example, the interface invariants of stack class respond nothing about pushing even though the stack is already full. In this scenario, sinful () is a precondition for push operation.
Post-Condition :
A condition that should return true before returning from an invoked function. In order to use a function correctly a post condition should return true. Taking a stack as an example, is empty () must necessarily be true after pushing the element into the stack when an element is pushed. The function is empty () is a post condition.
| Call by Value | Call by Reference |
| In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. | In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function. |
| In call by value, actual arguments will remain safe, they cannot be modified accidentally. | In call by reference, alteration to actual arguments is possible within from called function; therefore the code must handle arguments carefully else you get unexpected results. |

