In: Computer Science
What is a VOID function? How do local and global variables differ? Explain how the value-return function works.
//Please add comments for any queries
A void function is a type of function that does not returns a value after it gets executed. A function with a written type as void is called, is executed and then the control gets returned back to the caller without any return value.
Suppose, there is a function called print to print an element of an array. It will be called like this.
L1: print(); //There is no return value to the function.
As soon as the function is executed , the control comes back to L1.
Its definition looks like this,
void print()
{
//Function Definition
}
//Second part
Next , we have difference between local and global variable
A local variable is a variable that has a scope restricted to a specific block code or a function. A local variable can only be used within that function i.e no one can access that variable outside its block of code or function.
A global variable is a variable that is not limited to any speicific block code or a function. It can be used anywhere in that program by anyone. In other words, the scope of global variable is within the entire program and not just in any specific block.
Here is a C++ program for better understanding of the two
#include<iostream>
using namespace std;
int i_am_global=10; //Global variable
int main()
{
int i_am_local=5; //Local
variable
cout<<"Global variable:"<<i_am_global;
cout<<"Local variable:"<<i_am_local;
}
In this code, the variable i_am_global is outside any block of code or function and hence, is a global variable and can be seen to be accessible in main function whereas i_am_local variable is restricted to main only, and if we try to access it outside main then it will throw an error.
Note- If both local and global variable name are same, then we can access global variable value using scope resolution operator.
//Third Part
The working of a value returns function.
Let us consider a program for the understanding of the above
It is a simple program to check if a number is even or not.
#include<iostream>
using namespace std;
bool is_even(int x);
void main()
{
int x;
cout<<"Enter a number to check if it is even or odd";
cin>>x;
bool c=is_even(x); //Let us consider this
to be L1
if(c==0)
cout<<"Number is not even";
else
cout<<"Number is even";
}
bool is_even(int x) //Let us consider this to be
L2
{
if(x%2==0)
return 1;
else
return 0;
}
In this program, a number is taken as input and and function at L1 is called,you can see that the function has a return type of bool and has a parameter x i.e. the number to be checked,the control of the program now moves to L2 and the function is executed i.e. checked if is an even number or not.We can see that the function returns either of the two values i.e. 0 or 1 which is stored in c which is of type bool.So as soon as L2 is executed, control returns back at L1 and variable c of type bool stores the retuned value and executed the further program. In this way, the function of return value works.
Hope it helps.