Professional Documents
Culture Documents
x = x+10; }
y = x*x + (10/2);
stars (); //call to function
getchar(); return 0;
}
LOCAL VARIABLES
• Normally variables are local in scope - this
means they can only be used in the function
where they are declared.
• All variables declared inside functions are
local variables
– Known only in function defined
void demo ()
{
x = x + 20; // x has not been declared in demo
printf("in demo x = %d \n\n", x); // x = 30
}
Dr. Yousaf, PIEAS
Global Variables
• We can also declare global variables.
void demo (int x) // demo has its own x, so now local for this function.
{
x = x + 20;
printf("in demo x = %d \n\n", x); // x = 30
} Dr. Yousaf, PIEAS
Example: Global Variable
#include <stdio.h> void caltot (float num1,
float total = 0.0; float num2, float num3)
void caltot (float num1, float num2, {
float num3); //declaration total = num1 + num2 +
void printtot (); num3;
int main() }
{
float x = 4.5, y = 3.7, z = 8.1; void printtot ()
caltot(x, y, z); {
printtot(); printf(“Total is %f\n”, total);
}
printf(“Total is %f\n”, total);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Example: Previous Example Without
Global Variable // Error
#include <stdio.h>
void caltot (float num1, float num2, void caltot (float num1,
float num3); //declaration float num2, float num3)
void printtot (); {
int main() total = num1 + num2 +
{ num3; // error
float x = 4.5, y = 3.7, z = 8.1; }
float total = 0.0;
caltot(x, y, z); void printtot ()
printtot(); {
printf(“Total is %f\n”, total); printf(“Total is %f\n”, total);
getchar(); // error
return 0; }
}
Dr. Yousaf, PIEAS
Notes About Global Variables
– Global variables are also called extern
variables.
– Global variables are known in any
function
• A global variable is used when it must be accessible to
more than one function in a program.
• Global variables are often the most important
variables in procedural programs.
• However, global variables create organizational
problems because they can be accessed by any
function.
• The wrong functions may access them, or functions
may access them incorrectly.
Question:
Can there be a local and a global variable of
same name?
Ans:
Yes, if a function has a variable whose name is
same as previously declared global variable,
then the local variable will take precedence
inside such a function
(2) A palindrome is a number that reads the same from left to right or
from right to left. For example 121 , 222, 62226 are all palindrome
numbers.
Write a program (without function) that should accept three digits
number from the user. You code should tell whether the given number
is palindrome or not?
(3) Modify the above program such that it should accept three digits
number from the user. Write a function isPalindrome. Pass the
number provided by the user from main to this function. This function
should determine whether the number is palindrome or not? The
function should return the result to the main. The main should display
it. Dr. Yousaf, PIEAS
Home Take Assignment
A stack is a container of objects such that objects are placed on top of
each other just like dishes are placed on top of each other.
1‐push: This function should put the value of a variable to the stack.
2‐pop: This function should return the first element of the array
3‐show: The function should display the contents of the array stack
4-shift_right : This function should shift the elements of the array 1
place toward right i.e. element at index 0 will move to index 1 ,
element at index 1 will move to index 2 and so on till the last array
element is processed
5. void shift_left(void) : this function should shift the elements of the
array 1 place towards left i.e. element at index 1 will be shifted to
index 0, element at index 2 will be shifted to index 1.
ﺗﮑرار
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
• Termination
– Iteration: loop condition fails
– Recursion: base case recognized