17.02.2015 Views

CCS C Compiler Manual PCB / PCM / PCH

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Overloaded Functions<br />

Overloaded functions allow the user to have multiple functions with the same name, but they<br />

must accept different parameters.<br />

Here is an example of function overloading: Two functions have the same name but differ in the<br />

types of parameters. The compiler determines which data type is being passed as a parameter<br />

and calls the proper function.<br />

This function finds the square root of a long integer variable.<br />

long FindSquareRoot(long n){<br />

}<br />

This function finds the square root of a float variable.<br />

float FindSquareRoot(float n){<br />

}<br />

FindSquareRoot is now called. If variable is of long type, it will call the first FindSquareRoot()<br />

example. If variable is of float type, it will call the second FindSquareRoot() example.<br />

result=FindSquareRoot(variable);<br />

Reference Parameters<br />

The compiler has limited support for reference parameters. This increases the readability of<br />

code and the efficiency of some inline procedures. The following two procedures are the<br />

same. The one with reference parameters will be implemented with greater efficiency when it is<br />

inline.<br />

funct_a(int*x,int*y){<br />

/*Traditional*/<br />

if(*x!=5)<br />

*y=*x+3;<br />

}<br />

funct_a(&a,&b);<br />

funct_b(int&x,int&y){<br />

/*Reference params*/<br />

if(x!=5)<br />

y=x+3;<br />

}<br />

funct_b(a,b);<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!