- #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?

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Comp Sci
- Thread starter anonymous33
- Start date

- #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?

- #2

anorlunda

Staff Emeritus

- 11,207

- 8,621

We can't offer you tutorial help until you try it first yourself. Show us your work.

- #3

noname12345

- 31

- 4

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

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 );
}
```

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.

- #4

- 18,205

- 11,217

- #5

berkeman

Mentor

- 64,437

- 15,786

https://www.physicsforums.com/insights/recursion-and-when-to-use-not-use-it/

Share:

Comp Sci
Recursion double function

- Last Post

- Replies
- 4

- Views
- 708

- Last Post

- Replies
- 11

- Views
- 381

- Last Post

- Replies
- 10

- Views
- 702

- Last Post

- Replies
- 19

- Views
- 2K

- Last Post

- Replies
- 12

- Views
- 775

Comp Sci
Coding a Matlab Function

- Last Post

- Replies
- 10

- Views
- 683

Comp Sci
Question for "using" in C++

- Last Post

- Replies
- 3

- Views
- 510

- Last Post

- Replies
- 2

- Views
- 763

- Last Post

- Replies
- 2

- Views
- 362

- Last Post

- Replies
- 11

- Views
- 1K