Functions Coding

Cards (37)

  • Structured programming
    A program consists of individual program tasks which are performed by independent sections of program code
  • Advantages of structured programming

    • Easier to write, read, and understand
    • Easier to debug
    • Reuse of the same part of code at different places in the program without copying it
  • Planning a structured program for a contacts list management program
    1. Enter new entries (name and number)
    2. Read the existing contacts list from disk
    3. Prompt the user for new entry(ies)
    4. Add the new data to the list
    5. Save the updated list to disk
  • Planning a structured program for a contacts list management program
    1. Modify existing entries
    2. Read the existing contacts list from disk
    3. Modify one or more entries
    4. Save the updated list to disk
  • Function
    A self-contained block of code that performs a specific task
  • Every C/C++ program has at least one function - the main()
  • Function types
    • User-defined
    • Built-in
  • How a function works
    1. A function name is encountered
    2. Execution jumps to the function
    3. Statements inside the function are executed
    4. When function terminates, execution jumps back to the point where the call occurred
  • Function declaration (prototype)

    Has a return type, function name, and the parameter type list, ends with a semicolon
  • Function definition
    Has a return type, function name, and the parameter type list, contains a body enclosed between curly braces
  • Parameter
    An entry in a function header, serves as a "placeholder" for an argument
  • Argument
    An actual value passed to the function by the calling program
  • Local variable
    Variables declared inside a function, private to that particular function
  • z is 32.555000
  • The variable x under the function definition of half(…) is what we call a local variable
  • Local variables
    Variables declared inside a function, private to that particular function, distinct from other variables of the same name declared elsewhere in the program
  • The local variables of func (…) are y, a, b, rate, and cost
  • Upon termination of demo(…) the local variables x and y and the values stored to them were also terminated (or more like, gone)
  • Return statement
    Used to return a value from a function
  • A function can return at most one value only, but it can contain multiple return statements
  • If a function has multiple return statements, the first return statement executed is the only one that has any effect
  • To use (or call) a function, you specify the function's name and list the arguments to be passed to the function, enclosed between parentheses
  • A function call can either appear as an expression that returns a value, or as a statement (not required to return a value)
  • Recursion refers to a situation in which a function calls itself
  • The function illustrated below is said to be recursive: Foo () { /* code */ Foo (); /* code */ }
  • A paramater is used to pass information between functions
  • The two types of parameters are: Formal and Actual
  • Formal Parameters are the names given by the programmer when defining the parameter list.
  • Actual Parameters are the values that are passed into the formal parameters during the call to the function.
  • Pass By Value means that the value of an argument is copied into the corresponding formal parameter at the time the function is called.
  • The two types of parameter passin ing C: Pass by value and Pass by reference
  • In Pass by Reference, the address of the actual parameter is passed instead of its value.
  • When using pointers as arguments, we use & (address-of) operator to get the memory location of the variable.
  • One of the advantages of passing an argument by reference is because the function can modify the value of the argument variable
  • Function can only return a single value with the return statement
  • Functions are used to perform specific tasks within your program
  • A function that returns no values is called a void function