9.2 – Important points to remember

by subbu on November 12, 2013

Before starting next session in functions, we will discourse some important points here. These points are common in most of the languages used in now days. It helps to understand communication among the functions. I request you not to skip this session; it is worth investing some time here.

Point 1: We can’t use a variable without it’s declaration in any function. Every variable used in a function must be declared in the declaration section of that function (beginning of the function definition)

Example:

#include<stdio.h>
int main()
{
  int a,b;
  a=10;
  b=20;
  c=a+b;                      /* here c is an undefined symbol */
  printf("Sum %d",c);
  return 0;
}

Output:
Error: Undefined symbol ‘c’ in function main()

Example:

#include<stdio.h>
int main()
{
  sum();     /* calling the function sum */
  return 0;
}
sum()
{
  int a,b;              /* variable declaration section*/
  a=10,b=20;
  c=a+b;              /* here c is an undefined symbol */
  printf("Sum %d",c);
}

Output:
Error: Undefined symbol ‘c’ in function sum()

Example explained:

In a sub function, we can write any code like in main() function. We must declare all the variables used in the function at the beginning of the function definition. Here in this example c is not declared in the function sum(), which resulted error.

Point 2: If a variable is declared but, not assigned with any value then it is initialized with an unknown value called garbage value.

Example:

#include<stdio.h>
int main()
{
  int x;                /* declared but, not assigned with any value */
  printf("%d",x);
  return 0;
}

Output:
4203515   //It is the garbage value

Point 3: Multiple variables can’t be declared with the same name within a function.

Example:

#include<stdio.h>
int main()
{
  disp();
  return 0;
}
disp()
{
  int x=25;
  float x=12.50;
  printf("%d",x);
  printf("\n%f",x);
}

Output:
Error: Multiple declarations of symbol ‘x’ in function disp()

Example explained:

Here, the variable x is declared for two times in the sub function disp(),  which resulted error.

Point 4: Variables declared with in a function are called local variables. Variables declared in one function can’t be accessed from another function.

Example:

#include<stdio.h>
int main()
{
  int x=40;
  first();
  return 0;
}
first()
{
  printf("x=%d",x);
}

Output:
Error: undefined symbol “x” in function first()

Example explained:

local variable

Variable “x” is a local variable to the function main(). Its visibility is limited only to the main(), can’t be accessed from the first().

Example:

#include<stdio.h>
int main()
{
  int x=40;
  printf("x=%d",x);
  first();
  printf("\nx=%d",x);
  return 0;
}
first()
{
  x=x+50;
}

Output:
Error: Undefined symbol “x” in function first.

Example explained:

local variable 2

Here in this example variable “x” is a local variable to the function main(), can’t be accessed from the function first().
“x” is not even declared in the function first(), which resulted error.

Point 5: Multiple variables can be declared with the same name in different functions.

Example:

#include<stdio.h>
int main()
{
  int x=40;
  first();
  printf("\nx=%d",x);
  return 0;
}
first()
{
  int x=20;
  printf("x=%d",x);
}

Output:
x=20
x=40

Example explained:
There are two variables with the name ‘x’ in the above program but, one is in functions main() and another is in function first(). Both these are different and variable belongs to one function can’t be accessed by another.

Previous post:

Next post: