A
amanayin
trying to understand recursion
question why does this program run twice before
output is displayed
1; /* ex4-11.c program to find the power of a number */
2;
3; #include<stdio.h>
4;
5; int a,b,y;
6;
7; int powered(int x);
8;
9; int main(void)
10; {
11; printf("Enter a number: ");
12; scanf("%d",&a);
13; printf("Enter the number to find power: ");
14; scanf("%d",&b);
15;
16; powered(b);
17;
18; printf("The power of %d is %d\n",a,powered(b));
19; return 0;
20; }
int powered(int x)
{
if(x < 1)
return 1;
else
return a * powered(x -1);
}
________________________________________________________
________________________________________________________
Then if i change line 16 from (powered(b); to y = powered(b);
and the end of the printf statement as well it only runs
once before output is displayed
______________________________________________________
Out put for first example
______________________________________________________
GNU DDD 3.3.1 (i386-suse-linux), by Dorothea Lütkehaus and Andreas Zeller.
Copyright © 1995-1999 Technische Universität Braunschweig, Germany.
Copyright © 1999-2001 Universität Passau, Germany.
(gdb) break main
Breakpoint 1 at 0x8048384: file ex4-11.c, line 11.
(gdb) run
Breakpoint 1, main () at ex4-11.c:11
(gdb) step
(gdb) step
Enter a number: 4
(gdb) next
(gdb) next
Enter the number to find power: 5
(gdb) step
powered (x=5) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=4) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=3) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=2) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=1) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=0) at ex4-11.c:24
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
main () at ex4-11.c:18
(gdb) step
powered (x=5) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=4) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=3) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=2) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=1) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=0) at ex4-11.c:24
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
The power of 4 is 1024
main () at ex4-11.c:19
/home/glen/lsamc/week1/d4/ex4/ex4-11.c:19:317:beg:0x804840c
(gdb) step
(gdb) step
0x4003a8ae in __libc_start_main () from /lib/libc.so.6
(gdb) step
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program exited normally.
(gdb) step
The program is not being run.
(gdb)
question why does this program run twice before
output is displayed
1; /* ex4-11.c program to find the power of a number */
2;
3; #include<stdio.h>
4;
5; int a,b,y;
6;
7; int powered(int x);
8;
9; int main(void)
10; {
11; printf("Enter a number: ");
12; scanf("%d",&a);
13; printf("Enter the number to find power: ");
14; scanf("%d",&b);
15;
16; powered(b);
17;
18; printf("The power of %d is %d\n",a,powered(b));
19; return 0;
20; }
int powered(int x)
{
if(x < 1)
return 1;
else
return a * powered(x -1);
}
________________________________________________________
________________________________________________________
Then if i change line 16 from (powered(b); to y = powered(b);
and the end of the printf statement as well it only runs
once before output is displayed
______________________________________________________
Out put for first example
______________________________________________________
GNU DDD 3.3.1 (i386-suse-linux), by Dorothea Lütkehaus and Andreas Zeller.
Copyright © 1995-1999 Technische Universität Braunschweig, Germany.
Copyright © 1999-2001 Universität Passau, Germany.
(gdb) break main
Breakpoint 1 at 0x8048384: file ex4-11.c, line 11.
(gdb) run
Breakpoint 1, main () at ex4-11.c:11
(gdb) step
(gdb) step
Enter a number: 4
(gdb) next
(gdb) next
Enter the number to find power: 5
(gdb) step
powered (x=5) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=4) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=3) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=2) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=1) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=0) at ex4-11.c:24
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
main () at ex4-11.c:18
(gdb) step
powered (x=5) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=4) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=3) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=2) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=1) at ex4-11.c:24
(gdb) step
(gdb) step
powered (x=0) at ex4-11.c:24
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
(gdb) step
The power of 4 is 1024
main () at ex4-11.c:19
/home/glen/lsamc/week1/d4/ex4/ex4-11.c:19:317:beg:0x804840c
(gdb) step
(gdb) step
0x4003a8ae in __libc_start_main () from /lib/libc.so.6
(gdb) step
Single stepping until exit from function __libc_start_main,
which has no line number information.
Program exited normally.
(gdb) step
The program is not being run.
(gdb)