Functions

Cards (59)

  • A function is a block of code that performs a particular task.
  • STORAGE CLASSES
    There are two different ways to characterize variables:
    • by data type and by storage class
  • By data type – refers to the type of information represented by a variable
  • By storage class – refers to the permanence of a variable and its scope within the program, that is, the portion of the program over which the variable is recognized.
  • There are four storage-class specifications in C:
    1. Automatic variables or Auto
    2. External variables or Extern
    3. Static variables
    4. Registers
  • Automatic variables are always declared within a function and are local to the function in which they are declared; that is their scope is confined to that function.
  • Any variable declared within a function is interpreted as an automatic variable unless a different storage class specification is included within the declaration.
  • An automatic variable does not retain its value once the control is transferred out of its defining function.Therefore, any value assigned to an automatic variable within a function will be lost once the function is exited.
  • External variables, in contrast to automatic variables, are not confined to single functions. Their scope extends from the point of definition through the remainder of the program. Hence, they usually span two or more functions, and often an entire program.
  • External variables are recognized globally, they can be accessed from any function that falls within their scope. They retain their assigned values within this scope. Therefore, an external variable can be assigned a value within one function and this value can be used within another function.
  • A declaration for an external variable can look identical to a declaration for a variable that occurs inside a function or block. Such a variable is considered to be global to all functions declared after it, and upon exit from the block or function, the external variable remain in existence.
  • Static variables can be utilized within the function in the same manner as other variables. They cannot however be accessed outside of their defining function.
  • REGISTER VARIABLES - The storage class register tells the compiler that the associated variables should be stored in high-speed memory registers, provided it is physically and semantically possible.
  • Basically, the use of storage class register is an attempt to improve execution speed. When speed is concern, the programmer may choose a few variables that are most frequently accessed and declared them to be of storage class register.
  • Problem decomposition - refers to the heart of effective problem solving.
  • In C, the function construct is used to implement this "top-down" method of programming.
  • A function is a set of a program which performs a specific task. The task assigned to a function is perform whenever C encounters the function name.
  • Advantages of using function
    • Fits naturally with a top-down design approach
    • Helps to streamline the design of a program and prevents small details from obscuring the program logic
  • Function
    A blank box which performs a particular task within a program, accepts input and produces certain output
  • Ways functions can be used
    • Used more than once in a program
    • Used in several different programs, thereby sharing programming time
    • Plugging various block boxes into your program to accomplish various necessary tasks
    • Used for common tasks that appear regularly in totally unrelated programs
  • Advantages of using function
    • Provides a natural method for dividing a programming task among a team of programmers
    • Functions can be programmed as independent entities
    • Functions can be tested individually, which organizes and simplifies the process of debugging an entire program
  • The function declaration has a name of a function, the type of the value to be returned (if there are any) and the number and types of the arguments that must be supplied in a call of the function.
  • A function declaration may contain arguments names.
  • Function Declaration syntax:

    data_type_return function_name (parameter list)

    Example:
    int ccmit (int bsit, int bscs);
    void ccmit ();
    float ccmit (float x, float y);
  • Function Definition - The code that describes what a function does and it must nit be confused with the function declaration.
  • The function definition must have the following general form:
    data_type_return function_name (parameter list)
    { declaraction -local variables
    statement;
    }
  • The parameter acts as a placeholder for values that are passed when the function is invoked. Sometimes to emphasize their role s placeholders, these parameters are called the formal parameters of the function. The function body is a block, or compound statement, and it too may contain declarations.
  • Any variables declared in the body of function are said to be local to the function. Other variables are declared external to the function. These are called global variables.
  • Local variables - are the variables that are declared inside the function and it is referred to as Automatic Variables or the keyword auto.
  • A block of code is begun when an opening curly brace is encountered and terminated when a closing curly brace is found.
  • Global variable - known throughout the entire program and maybe used by any piece of code. Also they hold their values during the entire execution of the program. Global variables are created by declaring them outside of any function.
  • Storage for global variables is in fixed region of memory set aside for this purpose by the compiler.
  • The return statement:

    The return statement may or may not include an expression.
  • Syntax of return statement is:
    return; return(expression);
  • Function Prototypes:
    -should be declared before they are used.
  • A function prototype tells the compiler the number and the type of arguments that are to be passed to the function and the type of the value that is to be returned by the function.
  • Function prototype syntax:

    type function_name(parameter type list);

    • The parameter type list is typically a comma-separated list of types. Identifiers are optional.
  • A function call comes from either the main program or within a function. The function originating a function call is referred to as the calling function or calling environment.
  • A value parameter is a copy of a variable which is private to the function.
  • & is used as address operator means “the address of…”