Out of scope inside function but don't know how to fix it. tried global no luck

I have a script that sets a variable to the number messages to be transmitted and SHOULD loop around until I have transmitted all the messages. My problem is that I can’t figure out the scope of num_messages_to_be_transmitted.

I keep getting scope error inside the function message_transmission for num_messages_to_be_transmitted.

ERROR: UndefVarError: number_of_symbols not defined

here is the pseudocode of the script layout, NOT running code, I hope you can see the what I am trying to do. I tried setting global num_messages_to_be_transmitted =0 and got same error which really puzzled me. Can someone help me out.

num_messages_to_be transmitted = 0


function message_transmission(message)
              send message out the door
              num_messages_to_be_transmitted -= 1
end

num_messages_to_be_transmitted = queue_length

while num_messages_to_be_transmitted 
         message_transmission(next_message_in_queue)
end

Use a Ref:

julia> const CC = Ref(5)                                                                          
Base.RefValue{Int64}(5)                                                                           
                                                                                                  
julia> CC[]                                                                                       
5                                                                                                 
                                                                                                  
julia> CC[] = 6                                                                                   
6                                                                                                 
                                                                                                  
julia> CC[]                                                                                       
6              
 
julia> function g()                                                                               
       global CC                                                                                  
       CC[] = 0                                                                                   
       end                                                                                        
g (generic function with 1 method)                                                                
                                                                                                  
julia> g()                                                                                        
0                                                                                                 
                                                                                                  
julia> CC[]                                                                                       
0                                                                                                 
                                                                                                  
julia>                                                                                   
1 Like

For a beginner, this might be easier (working code):

num_messages_to_be_transmitted = 0


function message_transmission(message)
              println(message)
              global num_messages_to_be_transmitted -= 1
              println(num_messages_to_be_transmitted, " messages left in queue")
end

num_messages_to_be_transmitted = 5

while num_messages_to_be_transmitted > 0
         next_message_in_queue = "Hello $num_messages_to_be_transmitted"
         message_transmission(next_message_in_queue)
end
1 Like

Thank you AGAIN Prof
as always food for thought as I didn’t know anything about Ref.

theakson

Hi there @GHTaarn
excellent and thank you for taking the time. I’m still confused about the scope aspect of this. Why does putting the global at the top of the script not allow the function to access it?

thanks again for putting the effort into the code. I REALLY should have done that myself. Sorry

# CODE GENERATES ERROR

global num_messages_to_be_transmitted = 0


function message_transmission(message)
              println(message)
              num_messages_to_be_transmitted -= 1
              println(num_messages_to_be_transmitted, " messages left in queue")
end

num_messages_to_be_transmitted = 5

while num_messages_to_be_transmitted > 0
         next_message_in_queue = "Hello $num_messages_to_be_transmitted"
         message_transmission(next_message_in_queue)
end

1 Like

Because that’s not how the global annotation works. Where you are annotating it, it is already a global variable, ie in the global scope. The annotation means to use the variable in the global scope in the specific local scope instead of shadowing it v with a new variable. If it worked the way you might want by affecting all local scopes, a subsequent global annotation (remember julia code is dynamic) could break existing code. It would at least invalidate code and destroy its performance by suddenly causing it to access a non-const global. That would be really bad.

1 Like

thank you for more food for thought. I’m going to have to really work on the whole scope issue but I’ll get there. I hadn’t considered the performance aspect. thank you for pointing that out.