Writing a function

Hi, i need help answering a question from a past paper.

How do i write a function called "foo", that accepts a 1-dimensional array of double its size as an unsigned integer, passes back the root-mean-square of the array, and returns a bool.

I know how to write the body of the function, I just dont understand the definition part

The question is based on pseudocode, which is below if needed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if array size ≤ 0 
 return: false 
end if 
 
declare: aVal (as double) 
declare and initialise: sqrOfNums to 0 (as double) 
declare and initialise: sumOfSqrs to 0 (as double) 
 
for count = 0 to array size - 1 step 1 
 
 assign: aVal to value of current array element 
 assign: sqrOfNums to aVal x aVal 
 increment: sumOfSqrs by sqrOfNums 
 
end for 
 
assign: rms to sqrt(sumOfSqrs / array size) 
 
return: true 
Last edited on
What part of defining function prototype confuses you specifically?

ReturnType FunctionName(ArgumentType1 ArgumentName1, ArgumentType2 ArgumentName2, ...);
I understand the basic form of a function definition, but when it says the function should accept a 1-dimensional array, pass back the RMS, and return a bool, how do i do this?
You have the return type: bool
You have a parameter: double[] array
You have a parameter: std::size_t size
You have a parameter: double & rms
Read a good tutorial on returning by reference / pointer (pass back portion of the question) as opposed to standard return by value (returning a bool portion):
http://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/
Topic archived. No new replies allowed.