Question

In: Computer Science

What is a VOID function? How do local and global variables differ? Explain how the value-return...

What is a VOID function? How do local and global variables differ? Explain how the value-return function works.

Solutions

Expert Solution

//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.


Related Solutions

How does a value-returning function differ from the void functions? in pyhton
How does a value-returning function differ from the void functions? in pyhton
Explain the difference in the C memory model between global and local variables. How is each...
Explain the difference in the C memory model between global and local variables. How is each allocated and accessed?
What are the most common Incoterms used in global commerce and how do they differ in...
What are the most common Incoterms used in global commerce and how do they differ in risk and cost to the seller?
How do conceptions of CBDR differ between the Global North and the Global South? this is...
How do conceptions of CBDR differ between the Global North and the Global South? this is my third time posting this question, i need a clear and very simple answer in 2 paragraphs
What are global variables, how are global variables used in Arm architecture. Please feel free to...
What are global variables, how are global variables used in Arm architecture. Please feel free to use your own examples.
Do not use global variables. Note : The interleave function should not accept input or display...
Do not use global variables. Note : The interleave function should not accept input or display any output. The function should work with any size strings. Caution : Inclusion of unnecessary or unrelated logic or code segment will result in loss of points. Write C a program that has a main function and a utility function called merge. The main function should prompt the user to enter one short string (no longer than 20 characters) and hard code another string...
what are the elements of the company value chain (Micheal Porter) and how do they differ...
what are the elements of the company value chain (Micheal Porter) and how do they differ from the elements of an industry value chain? Which approach in your opinion, is better suited to analyzing a company's strengths, weaknesses, and opportunities?
How do global commodity chains differ the neoliberal global economy than the past? (limit to 200...
How do global commodity chains differ the neoliberal global economy than the past? (limit to 200 years) (8-12 sentences)
Discuss Global Promotion Strategies and how they differ between countries. What options do companies have in...
Discuss Global Promotion Strategies and how they differ between countries. What options do companies have in promoting their products? ?over 400 words?
Discuss Global Promotion Strategies and how they differ between countries. What options do companies have in...
Discuss Global Promotion Strategies and how they differ between countries. What options do companies have in promoting their products?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT