See a problem in else if statement

Dangling else problem

Image
Hello, This problem comes when we use else if statement and do some mistake more describe is given below......... Dangling else problem: One of the major problem with nested if is dangling else problem. The problem arises when both outer if statement inner if statement might pair with common else statement. Thus the compiler is not able to judge to pair the else with which statement. The solution to this problem is given by the compiler that it pairs else with the recent if thus sometimes giving wrong result.                                           Hence the programmer should put early bracket wherever required. This post is may very short because my only aim was to describe this problem. Thank you, Keep supporting............. 

Call by value and call by reference

hello friends here i will tell you what is call by value and call by reference, This is a topic of function,This topic will be used in c programming too much.


Call by value:

We had been calling function and passing arguments to them using call by value method.In the call by value method ,the called function creates new variables to store  the value of the argument passed to it.Therefore the called function uses a copy of the actual argument to perform its intended task. 
If the called function is supposed to modify the value of the parameters passed to it,then the change will be reflected only in the called function.In the calling function no change will be made to the value of the variables.This is because all the changes were made to the copy of the variable and not to the actual variables. 
 to understand this concept the code given below.The add( ) function accepts an integer variable num and adds 10 to it.In the calling function ,the value of  num=2.In add( ),the value of the num is modified to 12 but in the calling function the change is not reflected.

#include<stdio.h >
void add(int n);
main()
{
 int num=2;
printf("\n The value of num before calling the function =%d",num);
add(num);
printf("\n The value of num after calling the function=%d",num);
}

void add(int n)
{
n=n+10;
printf("\n The value of num after calling the function +%d",n);
}

Call by reference:

when the calling function passes arguments to the called function using call by value method, the only way to return the modified value of the argument to the caller is explicitly using the return statement.A better option when a function wants to modify the value of the arguments is to pass arguments using call by reference technique.In call by reference, we declare the function parameters as references rather than normal variables.When this is done any changes made by the function to arguments it receives are variable in the calling function.
       To indicate that an arguments is passed using call by reference,an asterisk (*) is placed after the type in the parameter list.This way, changes made to the parameter in the called function will then be reflects in the calling function.
        
                  Hence, in call by reference method , a function receives an implicit reference to the arguments,rather than a copy of its value.Therefore ,the function can modify the value of the variable and that change will be reflected in the calling function as well.The following programs uses this concept.
          To understand this concept, consider the concept the given below,

#include<stdio.h>
void add(int *n)
main()
{
  int num=2;
printf("|n the value of num before calling the function =%d",num);
add(&num);
printf("\n The value of num after calling the function =%d",num);
}
void add(int*n)
{
 *n=*n+20;
printf("\n the value of num in the called function=%d",*n);
}

output will be print in after calling function num +20 mean 20 will be add in 2 and print 22.


Thank you,

Comments

Popular posts from this blog

Libraray Fine Hackerrank solution in C || Balanced Brackets Hackerrank solution in C

Permutations of Strings hackerrank solution in C || Permutations of Strings

Introduction to C part 2