0% found this document useful (0 votes)
7 views35 pages

Lecture No

Uploaded by

alfatihalhssan1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

Lecture No

Uploaded by

alfatihalhssan1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

Lecture No(11) 1

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).

The variables can be categorized ,depending


on the place of their declaration as:
 internal(local) : declared within a particular
function.
3
external(global) : declared outside of any function.

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Scope Rules
File scope
 Identifier defined outside function, known in
all functions
 Used for global variables

 Function scope
 Can only be referenced inside a function
body 4

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Scope Rules
Block scope
 declared inside a block
 Block scope begins at declaration, ends at
right brace

Used for variables, function parameters


(local variables of function)
5

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Local variable
#include<stdio.h<
void fn(void(;

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

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


External variables
Variables that are alive and active
throughout the entire program .

Also known as global.

Can be accessed by any function in the


program.

Declared outside the function 7

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Global variable(External)
#include<stdio.h<
void change(void);
int num;
void main()
}num=10;
printf("\n num in main=%d",num;(
change();
printf("\n back in main=%d",num);
{
void change(void(
}num=20;
printf("\n in change,num=%d\n,num); 8
{
#include <stdio.h>
int fun1( void ); /* function prototype */
int fun2( void ); /* function prototype */
int fun3( void ); /* function prototype */
int x; /* global variable */
int main()
{
x = 10;
printf("x = %d\n", x );
printf("x = %d\n", fun1());
printf("x = %d\n", fun2());
printf("x = %d\n", fun3()); 9
}
‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬ 9
fun1( )
{ x = x+10;
return x;
}
fun2( )
{ int x =1;
return x; Output:
} 10
fun3( ) 20
{ x = x+10;
1
return (x);
} 30
10
}
‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬
External variables
void main()
{y=5;
}
int y;
void func1()
{y=y+1;}

main can’t access the variable y.


To solve this problem
• Declare the variable with the storage class extern 11
Lifetime Of Variables
In Functions
Longevity refers to the period during the
which a variable retains a given value during
execution of a program(alive).

Variables in C depend on the storage class


a variable may assume.

12

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Lifetime Of Variables
In Functions
A variable in C can have any one of the
four storage classes.
Automatic variables
External variables
Static variables
Register variables

13

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Automatic Variables
Declared inside a function in which they
are to be utilized.
Created when the function is called.
Destroyed when the function is exited.
Local(private)to the function in which they
are declared.
A variable declared inside the function
without storage class specification is by
default an atomic. 14

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Automatic Variables
To declare automatic variables explicitly
• use the keyword auto

The value of automatic variables can’t be


changed accidentally by what happens in
other functions in the program
• Use same variable name in different functions
in the same program.
15

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Example
Write a multifunction to illustrate how automatic
variables work
void function1()
{ int m=10;
printf(“%d\n”,m);
}
void function2()
{ int m=100;
function1(); 16
printf(“%d\n”,m);}
void main()
{ int m=1000;
function2();

printf(“%d\n”,m);
}

Output:
Output:
10
100
1000
17

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


External variables
void func1();

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

Multiple source files can share a variable


provided it is declared as an external variable.

Variables that are shared by two or more files


are global variables
• We must declare them in one file

• Explicitly define them with extern in other files.


19

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Multifiles Programs
File1.c File2.c
main() int m;
{ function2()
extern int m; {
int i; int i;
……… ………
} }
function1() function3()
{ int j; { int count;
……… ………
} } 20

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Static variables
The value of static variables persists until the
end of the program.

A variable declared static using static keyword


static int x;

A static variable may be internal or external,


depending on the place of declaration
21

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Static variables
void stat() A static variable is initialized
{ static int x=0;
only once,when the program
x=x+1;
printf(“x=%d\n”,x); is compiled.
}
main() What the output if we omit
{ the word static?!!!!
int i;
for(i=1;i<=3;i++)
Because in each call it initialized to zero
stat();
}
22

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Register variables
Tell Hint to place variable in high-speed
register

Good for often-used items (loop counters)


register int counter = 1;

23

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Recursion

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

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Recursion

The function launches a new copy of itself


(recursion step) to solve what it cannot do

Eventually base case gets solved


 Gets plugged in, works its way up and
solves whole problem

25

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


Recursion
 Example: factorials
• 5! = 5 * 4 * 3 * 2 * 1
• Notice that
 5! = 5 * 4!
 4! = 4 * 3! ...
• Can compute factorials recursively
• Solve base case (1! = 0! = 1) then plug in
 2! = 2 * 1! = 2 * 1 = 2; 26
 3! = 3 * 2! = 3 * 2 = 6;
‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬
The Factorial
fact( 4 )

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

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


The Fibonacci Series
 Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
• Each number is the sum of the previous two
• Can be solved recursively:
fib( n ) = fib( n - 1 ) + fib( n – 2 )
f( 3 )

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

‫ان االسلوب الذي نفكر به يحدد مسارنا في المستقبل‬


33
34
‫الحياة ألم يخفيه أمل‪,‬وأمل يحققه عمل‬
‫وعمل ينهيه أجل‪,‬ثم يجزى كل امرئ بما فعل‬
‫“ وعذري إن قصرت أننى إجتهدت ‘ وإ ن أخطأت‬
‫فلٌخ ير قصدت “‬

‫استودعكم اهلل‬
‫‪‬اسال اهلل ان يجمعنى بكم مرة ثانية إن لم يكن فى الدنيا‬
‫يجمعنا عند حوض النبى صل اهلل عليه وسلم فى الجنة‬
‫اللهم امين‬
‫‪35‬‬

‫اسالكم الدعاء لى بالثبات والتوفيق‬

You might also like