Program using function(call by value) to find square of a given number.
Code:
Sample Input and Output:
what is call by reference method ?
In call by reference,we pass the arguments to a function copies the address of an argument into the formal parameter.if any changes made in the formal parameter will reflect the actual parameter as well since it doesn't create copy but directly work on actual address,simply we can say that,original value is modified,argument pointers are passed to the functions just like any other value.
Explanation of above program
In this article,we are going to see how to write a program in which we can swap two number using call by reference method.
In the first line of code,we are including stdio(Standard Input Output) header file which enable us to use some of the most important built-in function like printf() and scanf().Onto the next line we are including conio.h header file which will help us to use getch() and clrscr() function which function help us to use hold the screen and clear the previously executed program screen respectively.
In the next line we are we are declaring square function which return type void which mean it can't return any value.As of now we have only declared it and not defined it.In the next line we have started our main function definition from which our program execution will start it's execution.
In the next line of our code,we have declared one variable with name a,which we will use to take user input,we usually use n to store any user input of int(Integer) type as I used to do in the previous article's but these(variable) are identifiers so it can be anything but should follow the rules of identifier.
In the next line,we have simply printed one statement 'Enter a Number' which will help user to get to known that we have to provide some input then only it will proceed because onto the next line we have wrote one scanf() function which will actually stop the program the execution unless and until the input is provided.
After that we are calling square function with a argument,it mean the value of a will be passed to the formal parameter of square.Once any user-defined function is called the cursor will directly goto the function definition of that function and start further execution
In the first line of square function we are declaring one variable with name n of type int(Integer),we will use this variable to store the squared value of a.In the next line we are doing.
n = a * a
The meaning of above operation it multiply the value of a with itself,indirectly we are calculating square of a by multiplying it with itself.for eg if the value of a is 3 then the value of n would be 9 since 3 * 3 = 9 .
In the next line of our code,we are printing which we have calculated with the help of following statement printf("The Square of entered number=%d\n",n); T=here 'The Square of entered number' statement will be printed as it is and the value of n(which contains square of n) will be printed.here we have used %d format specifier because the data type of variable n is int(Integer).
Once the above print statement is executed then the control of program will directly go back to the main function from where it is called and executed the further statement of main function.
On to the next line of main function,we have wrote getch() statement which will help us to hold the screen to see the output properly coz if we don't use this statement then the traditional compiler's like Turbo C,C++ compiler will not show the output properly because it output screen come and go so fast that we human will not be able to see it. Nowdays almost every compiler has this functionality by default,so we don't need to use it manually.
In the next line we have written return 0 statement which mean that the program executed successfully
friends.