You mean that the function f <!--Maybe you should think again about the name of the function...It does not inform the reader of what this function is designed to do --> is called twice inside the loop.In the program it is going to be called 2 times the number of loops that are going to be executed.
If you only want to call the function once inside the loop, you could modify the function to receive two pointers to x1 and x2.
In code this will be something like this
Code:
void f(float* x1, float* x2)
{
/* Careful when the asterisk operator */
/* is used for multiply or as a pointer!*/
/* In order to have the code cleaner I */
/* will write pointer to x enclosed in */
/* parentheses.E.g. *x == (*x) */
*x1 = (*x1) * ( (*x1) * (*x1) - 3.0 ) + 1.0;
*x2 = (*x2) * ( (*x2) * (*x2) - 3.0 ) + 1.0;
}
General Tip : Give your functions (and variables) names that denote their usage.This is increasing the readability of your code
Example
I will write two pieces of code, with no comments and i won't inform you for what this code is meant to do.
Read both and try to find out what is the goal of this code..Or to be more exact find out which one you would prefer to read.
Code:
#include <stdio.h>
int aTodayWrittenFunction(int* aVar , int anotherVar);
int aFunction(int var1, int var2);
int main(void)
{
int a0 = 5;
int a1[a0],a2,a3,a4;
for( a2 = 0 ; a2 < a0 ; a2++ )
a1[a2] = a2;
a3 = aTodayWrittenFunction(a1 , a0);
a4 = aFunction(a3 , a0);
printf("Value computed : %d\n" , a4);
return 0;
}
int aTodayWrittenFunction(int* aVar , int anotherVar)
{
int someVar,abcd = 0;
for(someVar = 0 ; someVar < anotherVar ; someVar++)
abcd += aVar[someVar];
return abcd;
}
int aFunction(int var1, int var2)
{
int a0;
a0 = var1/var2;
return a0;
}
The same code, but with different names of functions and variables
Code:
#include <stdio.h>
int computeSum(int* array , int length);
int computeAverage(int sum, int length);
int main(void)
{
int length = 5;
int array[length],i,sum,average;
for( i = 0 ; i < length ; i++ )
array[i] = i;
sum = computeSum(array , length);
average = computeAverage(sum , length);
printf("Value computed : %d\n" , average);
return 0;
}
int computeSum(int* array , int length)
{
int i,sum = 0;
for(i = 0 ; i < length ; i++)
sum += array[i];
return sum;
}
int computeAverage(int sum , int length)
{
int average;
average = sum/length;
return average;
}
I think that you will agree that the second one is "better" than the first one.Of course I am not saying that it is the best names that can be given.You might also think of other, more explanative names if you want.The ideal is to have the minimum length of names that fully explain the purpose of the variable or the function.As always in programming (and also life maybe) ,everything is a trabe of ( here length of word and how much info you include in it)
Also another tip.It is very useful for the others to see your code wrapped in code tags.
It's easy
[key]
/* YOUR CODE HERE */
[/key]
Replace key with the word code in order this to work.Try it out
Welcome to the forum,
G.Samaras