Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:31:11+00:00 2026-05-30T19:31:11+00:00

if there is a volatile variable passed in a function something like below… Does

  • 0

if there is a volatile variable passed in a function something like below…

Does this function always return a square value, I mean sometimes can the value change as there is a volatile variable declared.

Basically, I am trying to say is that the variable which is passed is a volatile. Hope, I am clear. But, if I make passing of a by reference that would change the whole scenario… am I right??

volatile int a;
int main()
{
   function(a);
   return 1;
}
int function b(int a)
{

    int x=a;

    int y=a;

    return x*y;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:31:13+00:00Added an answer on May 30, 2026 at 7:31 pm

    The volatile qualifier tells the compiler that the variable may change not that it will change. It cannot spontaneously change in any case; something has to cause it to change – even if that cause is external to the code.

    The code is erroneous in this case (or at least does not exemplify the effect of volatile in the way perhaps you intended) because the parameter passed to function() is not declared volatile. The fact that it has the same name as the global variable is perhaps confusing, but by assigning the global variable a to the function argument a, you have immediately decoupled it from the volatility.

    If the code were modified (and made valid and compilable) thus:

    volatile int a;
    
    int function( void )
    {
        int x=a;
        int y=a;
    
        return x*y;
    }
    
    int main()
    {
       function();
       return 1;
    }
    

    Then effect of volatile here is simply that the compiler will explicitly assign x and y without applying any optimisations that would otherwise be possible. Without the qualifier, the compiler might otherwise (when optimisations are enabled) reduce function() to just:

    int function( void )
    {
        return a * a ;
    }
    

    and might even in-line the entire function. Further, since the return value of function is not assigned to anything, the compiler would likley optimise the entire code to just

    int main()
    {
       return 1;
    }
    

    If a is volatile, then the assignments to x and y must occur if function() is called.

    The purpose of volatile is to indicate to the compiler that a variable may change outside of the code that the compiler has generated (not that it will change). It is typically used to ensure correct behaviour when a variable references either a hardware register, or memory that is shared between threads, processes, or even processors or cores within a processor. It can also be used to prevent “optimising out” of code that is required, (such as empty busy-wait loop counters for example). In your example (or rather my revision of it), a does not reference any such entity, so the code will behave as expected, but perhaps be less efficient that it might otherwise be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UDF non-volatile function in VBA, which requires a global variable (callback
I am wondering at the difference between declaring a variable as volatile and always
Is there any difference between a volatile Object reference and AtomicReference in case I
There is previous little on the google on this subject other than people asking
There are a few ways to get class-like behavior in javascript, the most common
I'm not quite sure if it's safe to spin on a volatile variable in
I occasionally use a volatile instance variable in cases where I have two threads
In C and C++ a variable can be marked as volatile , which means
Suppose I have a variable counter, and there are several threads accessing and setting
Consider the following scenario: There exists a globally accessible variable F . Thread A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.