• Functions

    What is a function?:

    • a function is a group of code that takes in some arguments (inputs) and returns a value (output)

    Why is a function important?:

    • you can reuse code
      • you can reuse the code inside the function
      • you can reuse functions
    • you can update a function and everywhere that calls the function will get the updated code
    • you can give a group of code a name to express what the group of code is trying to do
      • using functions with expressive names makes your code easier to read than using comments

    When is a good time to utilize functions?:

    • whenever you find yourself copy and pasting code
    • whenever you find yourself wanting to add a comment to explain what a section of code is doing
      • comments are notes to yourself and other programmers within the code
      • like Post-It notes on a word document
      • example usage:
        • // single line comment
        • /* multi-line comment: line 1
          multi-line comment: line2 */

    How to utilize functions?:

    • define a function
      • your_function is the function name
      • double your_function tells the compiler that the function is going to return a value with type double
      • your_function (int first_argument, double second_argument) is the argument list; the list of arguments (and the type of each argument) that the functions expects as inputs
    • call a function
      • your_function(first_argument, second_argument) is calls the function
      • your_function(first_argument, second_argument) are the arguments passed to the function
    • function prototype:
      • a function can be declared first with a function prototype, called, and then defined
      • when building a program using multiple .c files, function prototypes will make your life easier (and the program might not compile without it)
      • the function prototype tells compiler the name of the function (e.g. your_function, the inputs of the function (arguments and argument type, e.g. int first_argument & double second_argument), and the output of the function (the type of the value, e.g. double your_function)
    • arguments are values or variables that is passed to the function
    • read more at http://www.mycplus.com/tutorials/c-programming-tutorials/functions/
    • read more at http://en.wikibooks.org/wiki/C_Programming/Procedures_and_functions; it's a long read but contains a lot of useful information
    • read more at http://www.tutorialspoint.com/ansi_c/c_using_functions.htm; it's a long read but contains a lot of useful information
  • An Example Program that Uses a Function

    An example program (functions.c) that uses a function:

    • source code:
    • output:
      Hi from main
      -Hi from simple_function
      -You passed in an argument with the value of 1
      Bye from main
      -Hi from simple_function
      -You passed in an argument with the value of 2
    • things to note:
      • simple_function is used in multiple places
      • simple_function produced different outputs based on its inputs
      • simple_function returns a value that can be used by the program later on
      • simple_function's returned value does not always need to be used
      • main is shorter and less cluttered; compare for yourself
      • changes can be made in one place later on if things change
  • Scope

    What is scope?:

    Why is scope important?:

    • you and the program has a predefined way to mitigate naming collisions (i.e. calling a variable whose name is used to define variables in multiple places)
    • isolates variables and functions that should not be accessible, reducing the chance of unknowingly referring to a variable or function in some distant part of the program
  • Example Program to Illustrate the Effects of Scope

    scope.c:

    output:

    [Start of Main] variable: 20 
    [Start of function1] variable: 30 
    [Start of function1] passed_in_variable: 20 
    [End of function1] variable: 35 
    [End of function1] passed_in_variable: 25 
    [End of Main] variable: 20

    Things to Note:

    • the function uses the value of the arguments; the actual variable is unaltered
    • to alter the actual variable, pass pointers to the function; more on this later
  • Another Example Program to Illustrate the Effects of Scope

    global_scope.c:

    output:

    [In main] variable: 20 
    [In function] variable: 10

    Things to Note:

    • the more "local" variable "overrides" the more "global" variable
    • if there's no local variable the C language will search in outer function (main is also considered a function) until it reaches the global layer
    • use of global variables should be avoided; other functions might change the variable value unexpected and will cause bugs that are hard to debug
    • if you use global variables, try to store only values that is set at the beginning of the program and never modified afterwards (there's always exceptions to guidelines)
  • Write Your First Function

    write a function that:

    • takes in an integer, a decimal, a character as arguments
    • print out their values
    • return the decimal number as the output of the function
  • Assignment (1/2)

    Be sure to review the Pointers and Addresses lecture for next class

    Homework Problem 15.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_15_1.c
      • example:
      • zhaol_15_1.c
    • fill in the add function in add.c so it returns the value of the sum of the two inputs:

    • Test Case 1:
      Please enter in the first number:
      [user enters 3]
      Please enter in the second number:
      [user enters 6]
      The sum of your two numbers is: 9 
      
  • Pass By Value

    What is pass by value?:

    • pass by value is how you've been using functions

    Why is pass by value important?:

    • as you've seen, passing by value to a function leaves the original variable unaltered
    • you could store the value returned by the function or choose not to use it or a function might not return a value
  • Pass By Value Example

    What is happening when you pass an argument by value to the function:

  • Pass By Value Workarounds

    How have we been updating the variables?:

    • variable = add_one(variable); will effectively update variable

    How can we update multiple variables?:

    • write 2 functions and pass each variable separately, e.g. something like this:

      pass_by_value_workaround.c:

    • or pass both variables by reference to the function
  • Pass By Reference

    What is pass by reference?:

    Why is pass by reference important?:

    • it allows the function to update variable(s) directly
  • Pass By Reference Example

    What is happening when you pass an argument by reference to the function:

  • Pass By Value vs Pass By Reference

    Let us summarize the main differences between pass by value and pass by reference:

    Main Differences
    Pass By ValuePass By Reference
    calling the function function(main_variable);
    (pass in the variable value)
    function(&main_variable);
    (pass in the variable address)
    defining the function function(int function_variable);
    (initialize an int variable)
    function(int *function_variable_pointer)
    (initialize a pointer to an int)
    within the function function_variable++;
    (process as a variable)
    (*function_variable_pointer)++;
    (process as a pointer)
    effect on variable changes in the function will not affect the original variable changes in the function will take affect on the original variable
    analogous to
    • Person A passing information to Person B
    • the information passed along is that the most awesome students at UH Manoa are in EE160
    • when Person B takes the information and says "EE160 students rocks", the students of EE160 will not be affected
    • Person A passing information to Person B
    • the information passed along is that the most awesome students at UH Manoa are located in MSB114
    • when Person B goes to MSB114 and says "EE160 students rocks", the students of EE160 will be affected

    Let us visualize the differences between pass by value and pass by reference:

  • Update Your First Function

    write a function that:

    • takes in an integer address, a decimal, and an address to a character as arguments
    • modify the value at the integer address to be one more than its current value
    • modify the value at the character address to be 'a'
    • print out their values
    • return the decimal number as the output of the function
  • Assignment (2/2)

    Homework Problem 15.2, at the end of this problem you should submit to Laulima:

    • a file named UHusername_15_2.c
      • example:
      • zhaol_15_2.c
    • fill in the add_now function in add_now.c so it increments both variables by 1:

    • Test Case 1:
      Please enter in the first number:
      [user enters 4]
      Please enter in the second number:
      [user enters 8]
      The first number is now: 5
      The second number is now: 9