Advertisement
dmilicev

passing_arguments_to_functions.c

Dec 4th, 2019
196
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.18 KB | None
  1. /*
  2.  
  3.     passing_arguments_to_functions.c
  4.  
  5.     Passing arguments to functions by value and by reference.
  6.  
  7.  
  8.     You can find all my C programs at Dragan Milicev's pastebin:
  9.  
  10.     https://pastebin.com/u/dmilicev
  11.  
  12. */
  13.  
  14. #include <stdio.h>
  15.  
  16.  
  17. // int int_number is passed by reference
  18. // int_number is the address (pointer) where the integer value is located
  19. int function_2( int *int_number )
  20. {
  21.     int i;
  22.  
  23.     // display variable in function_2()
  24.     printf("\n\n function_2(): display variable in function_2() \n");
  25.  
  26.     printf("\n function_2(): int_number = %d ", *int_number );
  27.  
  28.     // change variable in function_2()
  29.     printf("\n\n function_2(): change variable in function_2() \n");
  30.  
  31.     // int_number is passed by reference, so value on address int_number become 2
  32.     *int_number = 2;
  33.  
  34.     // display variable in function_2() after changing
  35.     printf("\n\n function_2(): display variable after changing in function_2() \n");
  36.  
  37.     printf("\n function_2(): int_number = %d ", *int_number );
  38.  
  39.     printf("\n\n NOTICE that *int_number is changed in function_2() ! \n");
  40.  
  41.  
  42.     printf("\n\n");
  43.  
  44.     return 0;
  45.  
  46. } // function_2()
  47.  
  48.  
  49. // char array_of_characters[50] is passed by reference
  50. // char array_of_strings[5][50] is passed by reference
  51. // int array_of_int[3] is passed by reference
  52. // int int_number is passed by value (will not be changed after returning from function)
  53. int function( char array_of_characters[50],
  54.               char array_of_strings[5][50],
  55.               int array_of_int[3],
  56.               int int_number )
  57. {
  58.     int i;
  59.  
  60.     // display variables in function()
  61.     printf("\n\n function(): display variables in function() \n");
  62.  
  63.     printf("\n function(): array_of_characters = |%s| ", array_of_characters );
  64.  
  65.     for(i=0;i<5;i++)
  66.         printf("\n function(): array_of_strings[%d] = |%s| ", i, array_of_strings[i] );
  67.  
  68.     for(i=0;i<3;i++)
  69.         printf("\n function(): array_of_int[%d] = %d ", i, array_of_int[i] );
  70.  
  71.     printf("\n function(): int_number = %d ", int_number );
  72.  
  73.  
  74.     // call function_2() from function()
  75.     printf("\n\n function(): call function_2() from function() \n");
  76.     function_2( &int_number );
  77.  
  78.     // function(): display changed variable in function() after calling function_2()
  79.     printf("\n\n function(): display changed variable in function() after calling function_2() \n");
  80.     printf("\n function(): int_number = %d ", int_number );
  81.  
  82.     // function(): value int_number is changed by calling function_2()
  83.     // but will not be changed in main because is passed to function() by value (not by reference)
  84.     printf("\n\n function(): display changed variable in function() after calling function_2() \n");
  85.  
  86.     printf("\n\n function(): NOTICE value int_number is changed by calling function_2() \n but will not be changed in main \n because is passed to function() by value (not by reference) \n");
  87.  
  88.  
  89.     // change variables in function()
  90.     printf("\n\n function(): change variables in function() \n");
  91.  
  92.     sprintf(array_of_characters, "array_of_characters_changed_in_func_1");
  93.  
  94.     for(i=0;i<5;i++)
  95.         sprintf(array_of_strings[i], "array_of_strings_%d_changed_in_func_1", i+2);
  96.  
  97.     for(i=0;i<3;i++)
  98.         array_of_int[i] = 1 + i * i;
  99.  
  100.     int_number = 1;
  101.  
  102.  
  103.     // function(): display changed variables in function()
  104.     printf("\n\n function(): display changed variables in function() \n");
  105.  
  106.     printf("\n function(): array_of_characters = |%s| ", array_of_characters );
  107.  
  108.     for(i=0;i<5;i++)
  109.         printf("\n function(): array_of_strings[%d] = |%s| ", i, array_of_strings[i] );
  110.  
  111.     for(i=0;i<3;i++)
  112.         printf("\n function(): array_of_int[%d] = %d ", i, array_of_int[i] );
  113.  
  114.     printf("\n function(): int_number = %d ", int_number );
  115.  
  116.     printf("\n\n NOTICE that int_number is changed in function() ! \n");
  117.  
  118.  
  119.     printf("\n\n");
  120.  
  121.     return 0;
  122.  
  123. } // function()
  124.  
  125.  
  126.  
  127. int main()
  128. {
  129.     char array_of_characters[50];   // one string, array of 50 characters
  130.     char array_of_strings[5][50];   // 5 strings long 50 characters
  131.     int array_of_int[3];            // array of 3 int numbers
  132.     int int_number;                 // one int number
  133.     int i;
  134.  
  135.     // initialize variables in main()
  136.     sprintf(array_of_characters, "User_name");
  137.  
  138.     for(i=0;i<5;i++)
  139.         sprintf(array_of_strings[i], "User_%d", i+1);
  140.  
  141.     for(i=0;i<3;i++)
  142.         array_of_int[i] = i * i;
  143.  
  144.     int_number = 0;
  145.  
  146.     // display variables in main()
  147.     printf("\n\n main(): display variables in main() \n");
  148.  
  149.     printf("\n main(): array_of_characters = |%s| ", array_of_characters );
  150.  
  151.     for(i=0;i<5;i++)
  152.         printf("\n main(): array_of_strings[%d] = |%s| ", i, array_of_strings[i] );
  153.  
  154.     for(i=0;i<3;i++)
  155.         printf("\n main(): array_of_int[%d] = %d ", i, array_of_int[i] );
  156.  
  157.     printf("\n main(): int_number = %d ", int_number );
  158.  
  159.  
  160.     // call function() from main() with arguments
  161.     printf("\n\n main(): call function() from main() with arguments \n");
  162.     function( array_of_characters,
  163.               array_of_strings,
  164.               array_of_int,
  165.               int_number );
  166.  
  167.  
  168.     // main(): display variables after calling function() with arguments
  169.     printf("\n\n main(): display variables after calling function() with arguments \n");
  170.  
  171.     printf("\n main(): array_of_characters = |%s| ", array_of_characters );
  172.  
  173.     for(i=0;i<5;i++)
  174.         printf("\n main(): array_of_strings[%d] = |%s| ", i, array_of_strings[i] );
  175.  
  176.     for(i=0;i<3;i++)
  177.         printf("\n main(): array_of_int[%d] = %d ", i, array_of_int[i] );
  178.  
  179.     printf("\n main(): int_number = %d ", int_number );
  180.  
  181.     printf("\n\n NOTICE that int_number isn't changed in main() after calling function() \n because it is passed to function() by value, not by reference ! \n");
  182.  
  183.  
  184.     // call function_2() from main() with argument by reference (not by value)
  185.     printf("\n\n main(): call function_2() from main() with argument by reference (not by value) \n");
  186.  
  187.     // int_number is passed by referenece (not by value)
  188.     // &int_number is the address (pointer) where the integer value is located
  189.     function_2( &int_number );
  190.  
  191.  
  192.     // main(): display variable in main() after calling function_2() with argument passed by reference
  193.     printf("\n\n main(): display variable in main() after calling function_2() \n with argument passed by reference \n");
  194.  
  195.     printf("\n main(): int_number = %d ", int_number );
  196.  
  197.     printf("\n\n NOTICE that in main() int_number is changed \n because it is passed to function_2() by reference, not by value ! \n");
  198.  
  199.  
  200.     printf("\n\n");
  201.  
  202.     return 0;
  203.  
  204. } // main()
Advertisement
RAW Paste Data Copied
Advertisement