Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

46.functions (point5-7)

Point 5: Variables defined with in a function are called local variables. Visibility of these variables is limited to the function in which they are declared. Variable belongs to one function cannot be accessed into another function.

 

Example1:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

Error: undefined symbol "x" in function first()

 

Example explained:

Variable "x" is defined within the function main().

It can't be accesses from the first().

Because "x" is not declared within first(), it results error.

 

 

 

 

 

 

 

 

Example2:

                          

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

x=20

x=40

 

Example explained:

Variables "x" are declared within the main() and first(). Both are local variables to their functions.

 

Example3:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

Error: Undefined symbol "x" in function first.

 

Example explained:

Variable "x" belongs to main() can't be accessed into first().

"x" is not declared with in first() which results error.

 

Point 6: Variables declared out side all the functions are called global variable.

These variables are accessed into the functions defined after variable declaration.

 

Example1:

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

x=10

x=30

 

Example explained:

Variable "x" is a global variable can be accessed into both functions main() and first().

 

Example2:

 

 


Example2:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

x=40

x=90

Example explained:

The same variable "x" is accessed into both main() and first()

 

Example3:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

Error: Undefined symbol "x" in function main()

          Undefined symbol "x" in function first()

 

Example explained:

Variable "x" is declared after the main() hence can't be accessed into main()

 

Point7:  If the local and global variables are with the same name then local variable overrides the global variable with in the function.

 

 

 

 

 

 

 

 

 

 

 

 


Output:

x=20

 

Example explained:

The local variable "int x=20" overrides the global variable "int x=40" with in the function main()


0 comments:

Post a Comment