Sure. Here's what you do.
1. Take the existing code and move it out of the function it's in
2. If the code is dependent on any variables in the main routine, pass those as parameters
So the function would look like this:
void SortArray(float array[], int arraySize) //pass in the array and the length of the array
{
..float swap = 0; //holding variable --only used here so I moved it from main
..for (i=0; i< (a -1); arraySize++)
..{
....for(j = (i+1); j < arraySize; j++)
....{
......if (array[i] > array[j])
......{
........swap= array[i];
........array[i] = array[j];
........array[j] = swap;
......}
....}
..}
}
(obviously, you'll want to remove the periods... that's just here for formatting in Yahoo Answers.)
You'd call this function from main() like this:
SortArray(price, a);
I left the cout call in main, as this is part of the use of the array, not the sorting of the array. When you re-use the array sort routine later you won't necessarily want to print out the sorted values.
You'll notice that I took the liberty of renaming a few variables in your function. Here are the reasons:
1. To demonstrate that the name of the variable in the function doesn't need to match the name in the caller. So the main calls the array 'Price' but I called it 'array' in the function. A lot of programmers new to C++ don't see this right away. It's legal to name them the same, but they are DIFFERENT copies of the value.
2. One goal of a function is to have something you can reuse. 'Price' is specific to what Main is doing and your sort function can sort any array of floats.
3. I strongly advise staying away from short names. C++ allows names as long as you like, and descriptive names are much easier to maintain later than short names. You'll forget what 'a' was in two weeks, let alone two years. Longer names like 'arraySize' fix that problem. Descriptive names are often called "self-documenting" because you don't need comments to say what they're fore. The variables x, y, and i are exceptions... they have specific roles that are already self-documenting. X and Y are coordinates, and i is a loop variable. j is often a loop variable as well. Use it if you like, but I find i and j confusingly similar to the eye. :)
4. Personal preference, I avoid 'temp' in final code. I often use the word 'temp' in code I insert for debugging and will remove later, so if I see a variable called 'temp' I assume it's not needed in the finished product. But that's just me.
Also, your swap variable above is an int, while the array is floats. You'll want to change that around or you'll not only be sorting the array but also rounding it! :)
Good luck, and let me know if you need further clarification.