← Back to Home

CITS2002 - Lecture 3
Functions - Slides

Introducing Functions

  • Functions take an input and produce an output
    • A C function can only return one value
    • However, there are many ways to bypass this
  • C uses functions to structure code
    • This is unlike Java which uses “methods”
  • For C, you must have a main() function, which is the function that gets called in the command line
    • This must be included in every C program!

Typically, a C program will either return an EXIT_SUCCESS or and EXIT_FAILURE as a function output to the operating system

Why do we Require Functions?

  1. Group related statements under a name (improves readability)
  2. Prevent repetition of code
  3. Act as entry points for system calls (used by the OS)
  4. Enable modularity and code reuse (via libraries)
  5. Allow multiple processes to use the same code (via shared libraries)

Where do we Find Functions?

  • We can write functions in other files → then call them from main file
  • Collections of related functions are termed libraries of functions
  • There are many available 3rd-party libraries online

The Role of main()

main() should be constrained to:

  • Check the program’s command-line arguments
  • Report errors and then call exit(EXIT_FAILURE)
  • Call other functions to do actual work
  • Finally call exit(EXIT_SUCCESS) if all went well

Function Datatypes

  • A function must declare a return type
    i.e. what type of value is its output (int, bool, etc.)
  • A function does one of two things:
    1. Do something but return no value (void)
      e.g. write something to an output file
    2. Return a value of a specific type (int, bool, etc.)
  • A function defines its return type at the start:
    e.g. void output (char ch, int n)

Passing Parameters to Functions

  • Parameters must have declared types
    • Defined in function itself (see above)
  • Expressions are evaluated when passed through a function
  • During the execution of a function, the parameters are local variables of the function
  • Some functions do not require a parameter, those functions are declares as: void output(void)
    • e.g. calling global variables within function
    • This is generally not recommended

Do not assume that function parameters are evaluated left-to-right. The compiler will probably choose the order of evaluation which produces the most efficient code meaning order can vary.

The static Keyword

  • static is a keyword that can be written at the front of a function or local variable for different effects
    e.g. static void output( void )

Effect depends on what type of variable its in front of:

  • Global variable signifies that function is only visible within that file
    • Function is hidden from other files
  • Local variable variable retains value between calls to function
    • Variable still needs to be initialised

Functions Receiving Variable Number of Arguments

  • printf() can receive a variable number of arguments that can also be of different types
  • This only applies to a small set of standard functions

Arrays - Slides

Introducing Arrays

  • Arrays let us store multiple related values using one name
  • They’re helpful when variables are logically connected (e.g. grid coordinates, temperature, player stats)
  • Lets us avoids declaring many separate variables like x1, x2, x3…

1-Dimensional Arrays

  • Declare with square brackets: int myarray[N];
    • N→ indicates max # of elements within brackets
  • Indexed from 0 to N-1
  • Access with myarray[i] like scalar variables
  • Can use in expressions, assignments, function calls, etc.

Variable-Length Arrays

  • Once an array’s length is defined, it cannot be changed
    • Normally, its size is defined at compile time
  • However, we may not know what size an array should be until run-time
    • We can use a variable for size which can be defined later
      int size = get_input();
      char myarray[size];

Strings = Char Arrays

  • C does not have a basic datatype for stings
  • However, we can consider strings as an array of characters

CITS2002 - Lecture 5