Professional Documents
Culture Documents
Lecture No
Lecture No
PAL
Scope Of Variables
In Functions
The scope of variable determines over what
part(s) of the program a variable is actually
available for use(active).
Function scope
Can only be referenced inside a function
body 4
void main()
} int num=5;
fn() ;
printf("\nThe value of num in main=%d",num(;
{
void fn(void)
} int num=20;
printf("\nThe value of num in fn=%d",num(;
{ 6
12
13
printf(“%d\n”,m);
}
Output:
Output:
10
100
1000
17
void main()
{extern int y; /*external declaration*/
printf("\n %d",y);
func1();
printf("\n %d",y);
}
int y;
void func1()
{y=y+1;printf("\n %d",y);
18
}
ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل
Multifiles Programs
23
Recursive functions
Functions that call themselves
• Can only solve a base case
• Divide a problem up into
What it can do
What it cannot do
What it cannot do resembles
original problem 24
25
4 * fact( 3 )
*
3 fact( 2 )
* fact( 1 )
2
27
return 1
#include <stdio.h>
long fact( long );
int main()
{
long result, number;
printf( "Enter an integer: " );
scanf( "%ld", &number );
result = fact( number );
printf( “factorial %ld = %ld\n", number,
result );
return 0;
28
}
ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل
/* Recursive definition of function fibonacci */
long fact( long n )
{
long f;
if (n == 1 )
return 1;
else
f = n * fact(n -1 );
}
29
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
30
return 1 return 0
#include <stdio.h>
long fibonacci( long );
int main()
{
long result, number;
printf( "Enter an integer: " );
scanf( "%ld", &number );
result = fibonacci( number );
printf( "Fibonacci( %ld ) = %ld\n", number,
result );
return 0;
31
}
ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل
/* Recursive definition of function Fibonacci */
long fibonacci( long n )
{
if ( n == 0 || n == 1 )
return n;
else
return fibonacci(n - 1) + fibonacci(n -
2 );
}
32
استودعكم اهلل
اسال اهلل ان يجمعنى بكم مرة ثانية إن لم يكن فى الدنيا
يجمعنا عند حوض النبى صل اهلل عليه وسلم فى الجنة
اللهم امين
35