Calculating a math formula using void function-recursion in C

Click For Summary

Discussion Overview

The thread discusses how to write a function in C that calculates a mathematical formula using void function recursion. The conversation touches on various aspects of recursion, void functions, and the requirements for implementing such a solution.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant asks how to write a function that calculates a math formula using void function recursion in C.
  • Another participant emphasizes the need for the original poster (OP) to attempt the problem before seeking help.
  • A different participant critiques the OP's vague requirements and suggests that a void function complicates the process of obtaining results from recursion.
  • Examples of calculating the factorial using both iterative and recursive approaches are provided, highlighting the challenges of using a void function.
  • Concerns are raised about the safety of using pointers in a void function, particularly regarding the initialization of variables by the caller.
  • A participant points out the importance of following forum rules regarding assistance and attempts to solve problems.
  • A link to an external article on recursion is shared as a resource for the OP.

Areas of Agreement / Disagreement

Participants generally agree that the OP needs to clarify their question and make an attempt at solving the problem. However, there is disagreement on how to approach the use of void functions and recursion in C, with differing opinions on the feasibility and safety of such implementations.

Contextual Notes

The discussion highlights limitations in the OP's question, including vague terminology and unspecified mathematical formulas. The conversation also reflects differing views on the appropriateness of using void functions for recursive calculations.

anonymous33
Messages
1
Reaction score
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
:welcome:

We can't offer you tutorial help until you try it first yourself. Show us your work.
 
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   Reactions: anonymous33 and Klystron
@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   Reactions: QuantumQuest, Klystron and anorlunda

Similar threads

Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K