CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Passing parameters to functions

The examples we've already seen show how parameters are passed to functions:

  • a sequence of expressions are separated by commas, as in:

    a = average3( 12 * 45, 238, x - 981 );

  • each of these expressions has a datatype. In the above example, each of the expressions in an int.
  • when the function is called, the expressions are evaluated, and the value of each expression is assigned to the parameters of the function:

    
    float average3( int x, int y, int z )
    {
        return (x + y + z) / 3.0;
    }
    

  • during the execution of the function, the parameters are local variables of the function.

  • They have been initialized with the calling values (x = 12 * 45 ...), and the variables exist while the function is executing.
    They "disappear" when the function returns.

  • Quite often, functions require no parameters to execute correctly. We declare such functions with:

    
    void backup_files( void )            
    {
        .....
    }
    

    and we just call the functions without any parameters: backup_files();

 


CITS2002 Systems Programming, Lecture 4, p6, 31st July 2024.