Anda di halaman 1dari 4

#include "stdio.

h" void towers(int,char,char,char); void towers(int n,char frompeg,char topeg,char auxpeg) { /* If only 1 disk, make the move and return */ if(n==1) { printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg); return; } /* Move top n-1 disks from A to B, using C as auxiliary */ towers(n-1,frompeg,auxpeg,topeg); /* Move remaining disks from A to C */ printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg); /* Move n-1 disks from B to C using A as auxiliary */ towers(n-1,auxpeg,topeg,frompeg); } main() { int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n\n"); towers(n,'A','C','B'); return 0; }

Pass by value and Pass by Reference We have seen as to how to pass values to a function through arguments. Actually there are two ways to pass values to a function through arguments. These two methods are explained below with examples. Pass By Value:

Example: void check (int x) { //body of function } int main ( ) { int b = 10; check (b); } In this function, x is a parameter and b (which is the value to be passed) is the argument. In this case the value of b (the argument) is copied in x (the parameter). Hence the parameter is actually a copy of the argument. The function will operate only on the copy and not on the original argument. This method is known as PASS BY VALUE. So far we have dealt only with pass by value (except when passing arrays to functions).

// Pass by value illustration #include <iostream.h> int square (int x) { return x*x; } int main ( ) { int num = 10; int answer; answer = square(num); cout<<"Answer is "<<answer; // answer is 100 cout<<" Value of a is "<<num; // num will be 10 return 0; } You can see that the value of num is unchanged. The function square works only on the parameter (i.e. on x ). It does not work on the original variable that was passed (i.e it doesn't work on num).

The diagram makes it quite clear as to what happens when we call square(num). The following initialization takes place: int x = num; and the function square( ) operates only on a copy of num.

Pass By Reference
In pass by reference method, the function will operate on the original variable itself. It doesn't work on a copy of the argument but works on the argument itself. Consider the same square function example: // Illustration of pass by reference

#include <iostream.h> void square (int *x) { *x = (*x) * (*x); } int main ( ) { int num = 10; square(&num); cout<<" Value of num is "<<num; // Value of num is 100 return 0; } As you can see the result will be that the value of a is 100. The idea is simple: the argument passed is the address of the variable num. The parameter of square function is a pointer pointing to type integer. The address of num is assigned to this pointer. You can analyze it as follows: &num is passed to int *x, therefore it is the same as: int *x = &num; This means that x is a pointer to an integer and has the address of the variable num. Within the function we have: *x = (*x) * (*x); * when used before a pointer will give the value stored at that particular address. Hence we find the product of num and store it in num itself. i.e. the value of 100 is stored in the address of num instead of 10 which was originally present there. The diagram below illustrates the difference between pass by value and pass by reference. Now when we dereference x we are actually manipulating the value stored in num.

This is the pass-by-reference method.

Justify your answer which looping structure is more simple and useful in C with suitable example while statement while (expression) statement

In

the expression is evaluated. If it is non-zero, statement is executed and expression is re evaluated. This cycle continues until expression becomes zero, at which point execution resumes after statement.

The for is preferable when there is a simple initialization and increment since it keeps the loop control statements close together and visible at the top of the loop. This is most obvious in for (i = 0; i < n; i++).. The structure of the program reflects the form of the input: Each step does its part, and leaves things in a clean state for the next. The whole process terminates on the first character that could not be part of a number.

Advantages of For Loop 1. The advantages of keeping loop control centralized are even more obvious when there are several nested loops. 2) This tends to eliminate large amounts of disorder quickly, 3) Help in minimizing the work so later stages have less work to do. 4) The interval between compared elements is gradually decreased to one, at which point the sort effectively becomes an adjacent interchange method. 5) It is possible to place multiple expressions in the various parts.

Anda mungkin juga menyukai