Ad

FUNCTIONS

FUNCTIONS

 FUNCTIONS


  • There are two types of files: Header files and Library files.
  • Header files contain Declaration of functions whereas Library files contain function Definitions.
  • We can declare a function within another function but cannot define a function inside a function. 
  • Different functions can be defined and declared in various files.
  • Only 1 variable can be returned.
  • If a function's return type is int/char, we need not declare it. (by default compiler assumes the return type of function as int)
  • If the function does not have any arguments, it is a good practice to declare and define the function as follows:
    • void func_name ( void )
  • Formal parameters: Formal parameters are variables and its types which appear in the prototype of the function or method.
  • Actual parameters: Actual parameters are variables or expressions corresponding to a formal parameter that appears in the function or method call in the calling environment.
  • If the number of actual arguments is greater than the number of formal arguments (number of actual args > number of formal args) then the assignment from actual parameters to formal parameters starts from Left to Right and all other values are ignored.
                    e.g.        func(2,3);                    // function call

                                int func (int a)               // function definition
      • Here the value 2 will be assigned in variable a and 3 will be ignored by the compiler.
    • If the number of formal arguments is greater than the number of actual arguments (number of formal args > number of actual args) then the assignment from actual parameters to formal parameters starts from Left to Right and a random garbage value is stored remaining formal parameters.
                      e.g.        func(2);                                // function call

                                  int func (int a, int b)               // function definition
        • Here the value 2 will be assigned in variable a and garbage value will be stored in variable b.
      • Datatype mismatch: Suppose, a function accepts double type as an argument, and integer is given as actual parameter, then the compiler will forcefully carry out typecasting.i.e. implicit typecasting. If any complications occur, we will get the garbage value as a result.
                      e.g.        double sqrt ( double )
        • If we send an integer variable as a parameter, either the compiler will successfully give proper result by implicitly typecasting it into double, or it will give garbage value as result.
      • Order of evaluation of arguments is not defined in C. It is compiler dependent i.e. different compilers can choose different orders of evaluation
      • The return type of the main function is an integer. The Operating function calls the main function and when we return the main function, the call goes back to the Operating System. The main function is declared by the C compiler internally. The main function is defined by the programmer/user.
      • exit(0); is same as return 0; (the exit(int) function is same as return int function)
      • This is also a valid declaration of a function C:
                      return-type   func_name (arg1 , arg2 , arg3)
                      int arg1 , arg2 ;
                      float arg3 ;
                      {
                          ________
                          ________
                          ________
                          ________
                          ________
                      }
        • The older version of C used to have such types of declarations. It is still supported by modern-day compilers.
      • By default, All Global variables are initialized to 0.
      • When two variables of the same name (one local variable and one global variable) are present, the local variable will get precedence over global variable.
      • The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is the same as the local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of the class.

      Here, ::a and ::b will display the value initialized globally whereas a and b will display the value initialized locally.
      • As soon as the function returns and the control moves out, all the local variables are completely removed from the memory.
      • Static variables are not deallocated when the function returns. In static variables, their value remains the same between multiple function calls i.e. they are not removed from the memory when the function control returns and are kept as it is. 
        • Tip: Static Variables can count the number of times a function is called.
      • Recursion is less efficient than iteration many times. Recursion is time-consuming as more function calls are involved in recursion.
      • Towers of Hanoi
      • Lifetime of a variable is time between the creation and destruction of a variable.
      • Scope of a variable is where can a variable be accessed.
      • Auto is a keyword in C. When we do not define storage class of a variable, it is by default assigned auto storage class. If you do not initialize auto storage class variables while declaring, a random garbage value will be stored in the auto storage class variables. Any variable that is defined inside a parenthesis ({}) is an auto storage class variable by default. As soon as we come out of the block ({}), these are removed from the memory. The lifetime and scope of auto storage class variable is inside a parenthesis block ({}).
      • Extern is a keyword in C. It is called an external storage class. Extern storage class variables are useful when the variables are used by many functions or across multiple files.
                      e.g.             main()
                                        {
                                              ________
                                              ________
                                              ________
                                              ________
                                              ________
                                       }

                                         func1()
                                        {
                                              ________
                                              ________
                                              ________
                                       }

                                         int x=8;

                                         func2()
                                        {
                                              ________
                                              ________
                                              ________
                                       }
        • In the above example, x is a global variable but it can be accessed only by the function func2 because it was declared after main() and func1() function. To use it in main function and func1 function, we have to declare it in the function by external storage classes.
                                         main()
                                        {
                                              extern int x;
                                              ________
                                              ________
                                              ________
                                              ________
                                       }

                                         func1()
                                        {
                                              extern int x;
                                              ________
                                              ________
                                              ________
                                       }

                                         int x=8;

                                         func2()
                                        {
                                              ________
                                              ________
                                              ________
                                       }
        • You cannot initialize with an external storage class on the same line
                                  e.g.        extern int x=10;
        • This statement is invalid as the error will pop up as "error: ‘x’ has both ‘extern’ and initializer".
        • All external storage class variables are initialized to zero by default.
      • Static is a keyword in C. There are two types of static variables: local static variables and global static variables.
      • Local static variables: As soon as we come out of the block ({}), these are not removed from the memory, they remain in the memory until the program ends. The lifetime of the static storage class variable is until the program terminates and the scope of the static storage class variable is inside a parenthesis block ({}). Static variables are declared only once in a program i.e. when they are called the first time. If you do not initialize static storage class variables while declaring, the static storage class variables will be initialized to zero by default. The static storage class variables can only be initialized with a constant value/expression.
                          e.g.        static int x=10;
                                       static int x=(2+3);
        • The above examples are valid
                          e.g.        static int x=a;
        • The above example is invalid as a is not a constant, it is a variable.
      • Global static variables: A non-static global variable can be accessible in other files through extern keyword, but a static global variable cannot be accessible in other files. As soon as we come out of the block ({}), these are not removed from the memory, they remain in the memory until the program ends. The lifetime of the static storage class variable is until the program terminates. The scope of the static global variables is limited to the file it is created in. If you do not initialize static storage class variables while declaring, the static storage class variables will be initialized to zero by default. The static storage class variables can only be initialized with a constant value/expression.
      •                     e.g.        static int x=10;
                                         static int x=(2+3);
        • The above examples are valid
                            e.g.        static int x=a;
        • The above example is invalid as a is not a constant, it is a variable.
      • Register is a keyword in C. When we define register class variables, the compiler tries to store the memory of the variable in the register (cache memory). Register storage class can only be applied to local variables. The scope and the lifetime of the register class variables are the same as that of auto class variables. The only difference between the two is where they are stored. Auto class variables are stored in memory whereas register class variables are stored in CPU registers.
      • STORAGE CLASSES FOR FUNCTIONS
        • Extern: Extern is a default storage class for functions. It is useful when the functions are used in other functions or across multiple files.
          • e.g.                    extern void function1();
        • Static: Static storage class functions cannot be accessible in other files.

      0 Response to "FUNCTIONS"

      Post a Comment

      If you have any doubts, please let me know...

      Ads Atas Artikel

      Ads Center 1

      Ads Center 2

      Ads Center 3