Functions and Integer ============================ In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the basic properties of a integer ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is Call by Value ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is Call by Reference ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Integer : Syntax ` * :ref:`Integer : FAQs ` * :ref:`Integer : Basic Properties ` * :ref:`Integer : Call by Value ` * :ref:`Integer : Call by Reference ` * :ref:`Summary ` .. _funcs_n_ptrs_int_ex_0_0: .. tab-set:: .. tab-item:: Integer : Syntax .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow int c; .. _funcs_n_ptrs_int_ex_0_1: .. tab-set:: .. tab-item:: Integer : FAQs Consider a Integer .. code-block:: c int c; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions about a Integer .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many integers can be stored in ``c`` ? .. dropdown:: See Answer Number of Integers = 1 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bytes are there in this Integer ? .. dropdown:: See Answer Number of Bytes = 4 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the sizeof the Integer ? .. dropdown:: See Answer sizeof(a) = Number of Bytes = 4 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bits are there in this Integer ? .. dropdown:: See Answer Number of bits = sizeof(c) * 8 = 4 * 8 = 32 bits .. _funcs_n_ptrs_int_ex_1: .. tab-set:: .. tab-item:: Integer : Basic Properties Consider a integer .. code-block:: c int c; Then below are the properties of a integer =================== ======================================== Expression ? =================== ======================================== c ``c`` is a integer &c ``&c`` is address of integer ``c`` \*c NOT VALID sizeof(c) 4 Bytes sizeof(&c) 8 Bytes typeof(c) ``int`` typeof(&c) ``int *`` =================== ======================================== .. _funcs_n_ptrs_int_ex_2: .. tab-set:: .. tab-item:: Integer : Call by Value .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Integer passed to a function * Step 1 : Define a integer .. code-block:: c int c = 5; * Step 2 : Pass integer to a function .. code-block:: c fun(c); * Step 3 : Define a function ``fun`` .. code-block:: c void fun(int c) { } * Step 4 : Change integer inside function ``fun`` .. code-block:: c void fun(int c) { c = 10; } * See full program below .. code-block:: c #include void fun(int c) { c = 10; } int main(void) { int c = 5; printf("Before : c = %d\n", c); fun(c); printf("After : c = %d\n", c); return 0; } * See full program below .. code-block:: c Before : c = 5 After : c = 5 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow After function ``fun`` is called, * There are two stack frames * Stack frame of ``main`` * Stack frame of ``fun`` * variable ``c`` is created on stack frame of ``main`` * variable ``c`` is created on stack frame of ``fun`` * Even though name of variable ``c`` is same in ``main`` and ``fun``, they are two different variables in memory * Hence changing value of ``c`` inside function ``fun`` does not change the value of ``c`` inside ``main`` .. _funcs_n_ptrs_int_ex_3: .. tab-set:: .. tab-item:: Integer : Call by Reference .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Address of Integer passed to a function * Step 1 : Define a integer .. code-block:: c int c = 5; * Step 2 : Pass address of integer to a function .. code-block:: c fun(&c); * Step 3 : Define a function ``fun`` .. code-block:: c void fun(int *p) { } * Step 4 : Change integer inside function ``fun`` .. code-block:: c void fun(int *p) { *p = 10; } * See full program below .. code-block:: c #include void fun(int *p) { *p = 10; } int main(void) { int c = 5; printf("Before : c = %d\n", c); fun(&c); printf("After : c = %d\n", c); return 0; } * Output is as below .. code-block:: c Before : c = 5 After : c = 10 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us solve it with equations method ! * Rule 1 : Base Rule .. code-block:: c p = &c .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * RHS is actual parameter. In this case ``&c`` is actual parameter * LHS is formal parameter. In this case ``p`` is formal parameter * Rule 2 : Move ``&`` from RHS to LHS. This becomes ``*`` on LHS .. code-block:: c *p = c * Rule 3 : Changing ``*p`` also changes ``c``. Because we proved ``*p`` is equal to ``c`` .. _funcs_n_ptrs_int_ex_4: .. tab-set:: .. tab-item:: Summary ================================ ========================= ========================================================================== Function Call Function Definition Observations ================================ ========================= ========================================================================== fun(c) void fun(int c) { } Changing ``c`` inside ``fun`` DOES NOT change value of ``c`` in caller fun(&c) void fun(int \*p) { } Changing ``*p`` inside ``fun`` CHANGES value of ``c`` in caller ================================ ========================= ========================================================================== .. card:: See Also * Other topics of integer and functions * :doc:`./int` * :doc:`./int_sd_array` * :doc:`./int_dd_array` * :doc:`./int_td_array` * :doc:`./int_sp` * :doc:`./int_dp` * :doc:`./int_tp` * Current Module * :doc:`../funcs_n_ptrs` * Previous Module * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * Next Module * :doc:`../../memcpy_ptr/memcpy_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../const_ptr/const_ptr` * :doc:`../../void_ptr/void_ptr` * :doc:`../../array_of_ptr/array_of_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../function_ptr/function_ptr` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr` * :doc:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`