answersLogoWhite

0

How do you make a function repeat itself in C plus plus?

Updated: 8/11/2023
User Avatar

ZulfiqarMalik

Lvl 1
12y ago

Best Answer

There are three ways to do repetitions in C as far as I know. The first is to use recursion (a function which calls itself) the second being a for loop and the third being a while loop, though both of these are basically the same thing. Here's a demonstration of using these three methods to compute the x raised to the power of y: (note this isn't the most efficient way of calculating the power of a number but it's good enough for an example)

//Recursion:

int pow(int base, int pwr){

if(pwr == 0){

return 1;

}else{

return base * pow(base, pwr - 1);

}

}

//For loop

int pow(int base, int pwr){

int i;

int result = 1;

//The for loop initialises i to the value of pwr, then

//runs the loop, subtracting 1 from i each time until

//i fails the test (i > 0) at which point the loop finishes

for(i = pwr; i > 0; i--){

result *= base

}

return result;

}

//While loop

int pow(int base, int pwr){

int result = 1;

while(pwr > 0){

result *= base;

pwr--;

}

return result;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

Repetition structures in C++ are the same as those in C. Whenever we wish to repeat a section of code we "jump" back to the beginning of the section we wish to repeat. Typically the jump is conditional depending on the evaluation of a control expression:

repeat:

// ...

// statements to be repeated...

// ...

if (expression) goto repeat;

Alternatively, we can place the control expression within the body of the loop:

repeat: // ... // if (expression) goto end_repeat:

// ...

goto repeat;

end_repeat:

Whilst goto is efficient, it can make code difficult to read. Ideally, the control expression should be up front where it is easily seen. A while structure achieves this:

while (expression) {

// ...

// ...

}

If we wish a loop to execute at least once regardless of the evaluation of expression, we can use a do-while instead:

do {

// ...

// ...

} while (expression);

Note that a do-while loop is the equivalent of the first goto loop shown above.

Counted loops are one of the more common types of loop. We use a for loop structure for this:

for (x=0; x<10; ++x) {

// repeat statements 10 times...

// ...

}

Note that a for loop structure has three parts:

for (initialiser; expression; action) {

// ...

}

All three parts are optional but the semi-colons are not. To create an infinite loop we simply omit all three parts:

for (;;) { // means: for ever

// ...

}

An infinite loop can also be created using a whilestatement:

while (true) {

// ...

}

Note that all infinite loops must include at least one reachable break statement to exit the loop:

for (;;) {

// ...

if (expression) break;

// ...

}

We can also use a conditional continue to jump directly to the control expression:

for (;;) {

// ...

if (expression) continue;

// ...

}

Another way to break out of a loop is simply to returnfrom the function containing the loop. However, code with multiple return points can often be difficult to read. returnstatements are ideally placed at the end of a function.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

Repetition structures or Loops are the the segment of code which keep on repeating themselves till the time the condition provided by the coder is satisfied.
The loops are "For loop"
for( declaration of variables ; Condition ; Value Update)
{ //Code to be repeated }
____________________________________
While Loop
while( condition )

{ //Code to be repeated }
______________________________________
Do-While Loop
do
{ //Code to be Repeated}
while( condition )

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make a function repeat itself in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the trigonometric function for cotA plus B plus C?

cot(A+B+C) is, itself, a trigonometric function, so the question does not really make any sense!


When will you make a function inline in c plus plus?

yes,we can make function inline


Is 2x plus y equals 5 a function?

Yep. No values of X will ever repeat themselves.


How do you make plus or minus sign with MS keyboard?

press function (Fn) + P


How do you make internationaql calls?

Dial [1] plus the area code, and the number itself


Is y plus x2 plus 1 a function?

That is not a function, although it does involve the function of addition. A function is something that is done to numbers.


What are the building function in c plus plus?

There is no such term as "building function" in C++.


A c plus plus statement that invokes a function is known as?

...a function call.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


What is the polynomial for x2 plus 11x plus 30?

The actual equation itself is the polynomial. There is no polynomial for it, and your question doesn't really make sense.


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


Can reading plus repeat the story 3 times?

yes it can