Saturday 8 March 2014

An illustration of Calling a Function in C

When you call any function : there are two way to pass Value to it .  Today we discuss benefits of these two approaches .

CALL BY Value :   

In This Cause we just pass a copy of actual value and any change to this data in called function NOT change actual Value of The data in Caller function (which passes argument ).
Take an example : Swapping two number   without using third variable .



  1. #include <stdio.h>
  2. void swap (int, int);
  3. int main ()
  4. {
  5.         int a,b;
  6.         printf("An Illustration of Call by value\n");
  7.         printf("\nEnter the first number :\n");
  8.         scanf("%d",&a);
  9.         printf("\nEnter the second number :\n");
  10.         scanf("%d",&b);
  11.        
  12.     printf("\nCall by Value..  \n")
  13.                 swap(a,b); //calling function and print new value in that func;
  14.    
  15.     printf("\nIn main function ..");
  16.         printf("\nthe first number is %d \n",a);  //printing value of a & b in main 
  17.         printf("\nthe second number is %d\n",b);
  18.         return 0;
  19. }
  20. void swap (int x, int y)
  21. {
  22.         x=x+y; //swapping without using third variable 
  23.         y=x-y;
  24.         x=x-y;
  25.         printf("\nNow the first number is : %d \n",x);
  26.         printf("\nNow the second number is : %d \n",y);
  27. }

In this above example you can see first we call the function swap by method of call by value than than print its new values in function swap . But changing value in swap function does not alter value in main function .

CALL BY Reference  :

In this method we pass address of variable to be passed .  since now change made in values at this address obviously alter the value in caller function (here main )

Let us discuss above case : when we call by reference ....

  1. #include <stdio.h>
  2. void swap (int *, int *);
  3. int main ()
  4. {
  5.         int a,b;
  6.         printf("An Illustration of Call by reference\n");
  7.         printf("\nEnter the first number :\n");
  8.         scanf("%d",&a);
  9.         printf("\nEnter the second number :\n");
  10.         scanf("%d",&b);
  11.         swap (&a,&b);
  12.         printf("\nCall by Reference.. \n");
  13.         printf("\nNow the first number is %d \n",a);
  14.         printf("\nNow the second number is %d \n",b);
  15.         return 0;
  16. }
  17. void swap (int *x, int *y) //receive address in pointer 
  18. {
  19.         *x=*x+*y; //swapping without using third variable 
  20.         *y=*x-*y;
  21.         *x=*x-*y;
  22.         printf("\nNow the first number is : %d \n",*x);
  23.         printf("\nNow the second number is : %d \n",*y);
  24. }

you can see output ... . actual value in main function also changed .


Output for above two program is :


Thanks : Have a Nice Day

9 comments:

  1. My brother suggested I might like this web site. He was
    entirely right. This post truly made my day. You cann't imagine just
    how much time I had spent for this information! Thanks!


    Look at my webpage: Louis Vuitton Sale

    ReplyDelete
  2. Have you ever considered about including a little bit more than just your articles?
    I mean, what you say is important and everything. However think about if
    you added some great pictures or videos to give your posts more, "pop"!
    Your content is excellent but with images and videos, this website could certainly be one of
    the greatest in its field. Awesome blog!

    My weblog - Cheap New Balance

    ReplyDelete
  3. This is the right site for everyone who hopes to find out about this topic.
    You know so much its almost tough to argue with you (not that I personally would want to…HaHa).
    You definitely put a fresh spin on a topic which has been discussed for many years.

    Great stuff, just great!

    My site - Christian Louboutin Heels

    ReplyDelete
  4. It's fantastic that you are getting thoughts from this article as well as from our discussion made
    here.

    Here is my blog :: Fake Louis Vuitton Bags

    ReplyDelete
  5. I am regular reader, how are you everybody?
    This post posted at this site is actually good.

    my blog Beats By Dre

    ReplyDelete
  6. Hi there to all, how is everything, I think every one is getting
    more from this website, and your views are pleasant designed for new
    users.

    Visit my site Louis Vuitton Online

    ReplyDelete
  7. Does your blog have a contact page? I'm having trouble
    locating it but, I'd like to shoot you an email. I've got some ideas for your blog
    you might be interested in hearing. Either way, great website
    and I look forward to seeing it improve over time.


    my web site Beats By Dre (http://beats-australia.rr32.com/)

    ReplyDelete
  8. Valuable information. Fortunate me I discovered your website by accident, and
    I'm shocked why this twist of fate did not came about earlier!
    I bookmarked it.

    Also visit my webpage ... Dre headphones

    ReplyDelete
  9. With havin so much content and articles do you ever run into any problems
    of plagorism or copyright infringement?
    My website has a lot of completely unique content I've either
    authored myself or outsourced but it seems a lot of it is popping it
    up all over the web without my permission. Do you know any techniques to help protect against content
    from being ripped off? I'd certainly appreciate it.

    My blog post :: Replica Christian Louboutin

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets