Calculating a math formula using void function-recursion in C

In summary, the conversation discusses how to write a function that calculates a math formula using void function-recursion in C. The speaker suggests using a non-void function instead of a void function since it makes the process easier. They also provide an example of using recursion to calculate the factorial of a number. The speaker warns that using a void function requires the caller to initialize the variable properly before calling the function.
  • #1
anonymous33
1
0
No Effort - member warned that some effort must be shown
Homework Statement
C
Relevant Equations
Recursion
how can i write a function that calculate math formula using void function-recursion in C?
 
Physics news on Phys.org
  • #2
:welcome:

We can't offer you tutorial help until you try it first yourself. Show us your work.
 
  • #3
Your question mixes up a bunch of different, vague requirements and generic terms; but this might help you.

Firstly you ask for "... void function ...". A void function doesn't have a return value, so you're making life difficult by specifying that. Not only do you need to find an alternative way to obtain the final result, it is in the very nature of "recursion" that each level of recursion needs access to the intermediate results from the deeper levels.

You want to "calculate math formula", but do not specify what formula?

You say you want to use "function recursion", but only a subset of formulae lend themselves to recursive solutions.

So, let's start by picking an example formula: factorial. factorial( n ) =: n* n-1 * n-2 * n-3...

This can be computed in C without recursion or void return as:
Code:
int factorialIterative( int n ) {
    int answer = n;
    while( n > 1 ) {
        n -= 1;
        answer *= n;
    }
    return answer;
}

Using recursion and non-void function:
Code:
int factorialRecursive( int n ) {
    if( n == 1 ) return 1;
    return n * factorialRecusive( n-1 );
}
(That's pretty much the standard form for recursion in C.)

If you really insist on making life difficult by using a void function, then you need to have a way to return the computed result to the caller. One way to to that is to pass a pointer to a variable in the calling code that the function can store the result to.

Code:
void factorialRecursiveVoid( int *answer, int n ) {
    if( n == 1 ) return;
    *answer *= n--;
   return;
}

But that is dangerous as it requires the caller to ensure that the variable answer is initialised to (exactly) 1, before calling the function, and there is no reasonable solution to avoiding that statutory requirement of the caller; nor any reasonable mechanism by which the recursive function could verify it.
 
  • Informative
  • Like
Likes anonymous33 and Klystron
  • #4
@Buk, it's nice that you want to be helpful but seems like maybe you need to reread the rules yourself. As @anorlunda CLEARLY pointed out, we don't give help until the OP has made an attempt (and shown it here).
 
  • Like
Likes QuantumQuest, Klystron and anorlunda

Related to Calculating a math formula using void function-recursion in C

1. How do I create a void function-recursion in C to calculate a math formula?

To create a void function-recursion in C, you will need to define a function that calls itself within its own body. This allows the function to repeat the same calculation multiple times until it reaches a base case, which will stop the recursion. The base case should be a simple calculation that can be easily solved without recursion.

2. What is the benefit of using void function-recursion in C over other methods of calculation?

Void function-recursion can be a useful tool for solving complex mathematical formulas because it allows for a more efficient and concise code. It also reduces the need for multiple loops and conditional statements, making the code easier to read and debug. Additionally, void function-recursion can often be more accurate than other methods, as it can handle a larger number of iterations without rounding errors.

3. Can void function-recursion in C handle all types of mathematical formulas?

Yes, void function-recursion can handle any type of mathematical formula. As long as the formula can be broken down into smaller, simpler calculations, recursion can be used to solve it. However, it is important to consider the efficiency and accuracy of using recursion for certain formulas, as it may not always be the best approach.

4. How do I avoid infinite recursion when using void function-recursion in C?

To avoid infinite recursion, you must make sure that your function has a base case that will eventually be reached. This base case should stop the recursion and return a value without calling the function again. You should also carefully consider the input parameters and ensure that they are adjusted with each recursive call to eventually reach the base case.

5. Are there any limitations to using void function-recursion in C for mathematical calculations?

One limitation of using void function-recursion in C is that it can be memory-intensive. Each recursive call adds a new stack frame to the memory, which can quickly add up if the number of iterations is large. Additionally, recursive functions may not always be the most efficient approach for solving certain formulas, so it is important to consider other methods as well.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
932
  • Engineering and Comp Sci Homework Help
Replies
3
Views
972
  • Engineering and Comp Sci Homework Help
Replies
3
Views
771
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top