When using any of the built in peripherals like the ADC or SPI e.t.c specific pins are already designed to be used as input or output for specific functions. e.g using spi_write shall drive the SDO and SCK pins on the PIC. However, should the I/O direction of the involved pins always be set before these functions are called? i.e should I explicitly set the pin numbers corresponding to SDO and SCK as output before I call the spi_write function? The spi_write is found in the CCS PIC C Compiler but I am sure that similar functions also exists in other tools.
Yes. Sometimes it is given in datasheet of PIC itself. For example in datasheet of PIC18F452, in UART section, there it is specified that "TRISX bits should be defined before using this module, it should be output For TX and input for RX". So it is good practice to define data direction before using peripherals.
Combined with the ANSEL registers, it's good practice to define both (TRIS and ANSEL) even if they are using power-up defaults. Clears up the documentation.
It is difficult to say "always". The behaviour of the compiler should be examined, check the asm output produced. If you do not declare anything about port directions, CCS C is willing to set/clear tris registers according to your intention. However, if you declare #use fast_io() you must set tris registers, the compiler does not modify the registers so that your settings stay.