Tuesday, October 12, 2010

Software Engg: Introduction to Programming:Variables use In Funtion

Lecture Number : 4
Topics: External Variables And Static Variables In scope of Functions


External Variables

So far we have used variables that that were accessible to only functions using them. These variables are called local variable. They exists only when function is used, and are automatically destroyed when control goes out of function. A variable defined outside a function is called a Global Variable. A Global Variable can be used and changed in any function. Since a Global Variable can be used by any function, they can be used to share information between functions. When a variable is available to a function, it is said to be In Scope. When a variable is unavailable to a function, it is said to be Out Of Scope.


Don't Use Global Variables

Global Variables seem like a good idea, allowing simple sharing of information between functions. It is unclear from a function call that a variable is being used or changed. Global Variables make debugging and maintaining a program very difficult. Global Variables are considered BAD programming practice!

Functions Parameter List

If we can’t use Global Variables, how can we share information between functions?
An answer is a function prototype is made up of three things; return type, identifier and the parameter list. The return type can be used to pass a single value from a called function to the calling function. The Parameter List can be used to pass many values from a calling function to a called function.

Passing Parameters with Function Call
In a function call, values can be sent by a calling function to a called function via the Parameter List. These passed values appear inside the call operator ( ). Example:  MyFun ( 200 );  passes a single int  to the function MyFun. Example:  MyFun ( 200 , ‘x’ );  passes an int and a char to function MyFun.
  
Receiving Information from Function Call

A function call can pass one or more parameters to a called function via the parameter list:  MyFun ( 200, ‘x’); The parameter list in the Function Definition declares variables which store these values, and take on the values passed. Example:  int MyFun( int x, char c ) receives an int and a char from the call and stores them in x and c. These variables can now be used in the function.

Passing By a Value


When we pass a value to a function, a new variable is declared in the called function’s Definition which holds the passed value. Since this variable is local to the called function, changing its value will not impact the original value in the calling function. This is known as Passing By Value (or copy). In C functions, everything is passed by value.

Pre-processor Directives

In a C program, we have some other options that allow us to make our programming more useful and easier to maintain. Preprocessor Directives allow the compiler to make changes to our program BEFORE we compile it. Preprocessor Directives always begin with # and are not statements, so they do not end in a semicolon.

#include and #define.
  
  • #include allows us to include other files in our C program,
  • #define works like a “replace” in a word processor, allowing us to change something into something else before compile. 
                External Variables Example
 
          #include<conio.h>
          #include<stdio.h>
          int num;
          void evenodd();
          void neg();
          void main()
          {
                          printf("Enter a number\n");
                          scanf("&d",&num);
                          evenodd();
                          neg();
          }
          void evenodd()
          {
                          if(num%2)
                               printf("The number is odd\n");
                          else
                               printf("The number is even\n");
          }
          void neg()
          {
                          if(num<0)
                                printf("The number is Negative\n");
                          else
                               printf("The number is Positive\n"); 
           Static Variables Example
          #include<conio.h>
          #include<stdio.h>
          void fun();
          void fun1();
          void main()
          {
                          int a;
                          printf("Enter a value\n");
                          scanf("%d",&a);
                          printf("%d \n\n",a);
                          fun();
                          fun1();
                          fun();
                          fun1();
          }
          void fun()
          {
                          int a=1;
                          printf("This is %d time in fun\n\n",a);
                          a++;
          }
          void fun1()
          {
                          static int b=1;
                          printf("This is %d time in fun1\n\n",b);
                          b++;
          }

No comments:

Post a Comment