by Dinesh Thakur Category: Function and Pointer

This program defines a function named func which has two parameters, a and b of type int. Within its body, this function first prints these parameter values, initializes them to 30 and prints them again. The main function declares two variables, a and b of type int. It first prints the values of these variables and then calls the function func with variables a and b as its arguments. Finally, the main function prints the values of variables a and b again.

Observe from output lines 2 and 3 that the values of variables a and b are passed to the function and are then initialized to 30. However, the values of variables a and b in the main function are unaffected after the function call. This is expected, as function func uses the default call by value mechanism, making a and b input-only parameters. Also note that although the function parameters and the argument variables have same name, they are different entities, and modifying the function parameters does not affect the argument variables.

#include <stdio.h>

void func(int a, int b);

void main ()

{

         int a = 10, b = 20;

         clrscr();

         printf("In main(), before function call : a = %d b =%d\n",a,b);

         func(a, b);

         printf("In main(), after function call : a = %d b = %d\n",a,b);

         getch();

}

         void func(int a, int b)

        {

             printf("In func(), before initialization: a= %d b =%d\n",a,b);

             a = b = 30;

             printf("In func(), after initialization : a %d b =%d\n",a,b);

        }

        C Program Call By Value



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles