in Education by
I've spent the past few hours reading around in here and elsewhere, as well as experimenting, but I'm not really understanding what I am sure is a very basic concept: passing values (as variables) between different functions. For example, I assign a whole bunch of values to a list in one function, then want to use that list in another function later: list = [] def defineAList(): list = ['1','2','3'] print "For checking purposes: in defineAList, list is",list return list def useTheList(list): print "For checking purposes: in useTheList, list is",list def main(): defineAList() useTheList(list) main() Based on my understanding of what function arguments do, I would expect this to do as follows: Initialize 'list' as an empty list; call main (this, at least, I know I've got right...) Within defineAList(), assign certain values into the list; then pass the new list back into main() Within main(), call useTheList(list) Since 'list' is included in the parameters of the useTheList function, I would expect that useTheList would now use the list as defined by defineAList(), NOT the empty list defined before calling main. However, this is obviously a faulty understanding. My output is: For checking purposes: in defineAList, list is ['1', '2', '3'] For checking purposes: in useTheList, list is [] So, since "return" obviously does not do what I think it does, or at least it does not do it the way I think it should... what does it actually do? Could you please show me, using this example, what I would have to do to take the list from defineAList() and use it within useTheList()? I tend to understand things better when I see them happening, but a lot of the examples of proper argument-passing I've seen also use code I'm not familiar with yet, and in the process of figuring out what's going on, I'm not really getting a handle on this concept. I'm using 2.7. ETA- in the past, asking a similar question, it was suggested that I use a global variable instead of just locals. If it will be relevant here also- for the purposes of the class I'm taking, we're not permitted to use globals. Thank you! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
This is what is actually happening: global_list = [] def defineAList(): local_list = ['1','2','3'] print "For checking purposes: in defineAList, list is", local_list return local_list def useTheList(passed_list): print "For checking purposes: in useTheList, list is", passed_list def main(): # returned list is ignored returned_list = defineAList() # passed_list inside useTheList is set to global_list useTheList(global_list) main() This is what you want: def defineAList(): local_list = ['1','2','3'] print "For checking purposes: in defineAList, list is", local_list return local_list def useTheList(passed_list): print "For checking purposes: in useTheList, list is", passed_list def main(): # returned list is ignored returned_list = defineAList() # passed_list inside useTheList is set to what is returned from defineAList useTheList(returned_list) main() You can even skip the temporary returned_list and pass the returned value directly to useTheList: def main(): # passed_list inside useTheList is set to what is returned from defineAList useTheList(defineAList())

Related questions

0 votes
    What are the different ways of passing parameters to the functions? Which to use when?...
asked Jan 18, 2021 in Technology by JackTerrance
+1 vote
    What are the different ways of passing parameters to the functions in C Programming? Which to use when?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I am starting to work with python again after 8 years. I am trying to do the program with BeautifulSoup and an ... '__main__': main() Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it ... xrange(0, 20): Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    I'm working on a mini text role playing game where you must survive dinner with the king. I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 8, 2022 in Education by JackTerrance
0 votes
    If there are more than one file(main.lua) which contains code and I declare some variables and functions ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    I have a csv file and I am reading this file in python using pandas. I want to read each row of ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a python variable how can I see that it's unsigned 32 bit or signed 16 bit, etc? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    Is it mandatory to include underscores in variables in Python? For example, can I put myVariable in place of my_variable? Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Which of the following functions is a built-in function in python? a) factorial() b) print() c) seed() d) sqrt()...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Explain split() and join() functions in Python?...
asked Dec 7, 2020 in Technology by JackTerrance
0 votes
    ____________ will have the objects only after passing quality checks in QAS. A. Production B. Development C. Quality...
asked Feb 20, 2023 in Technology by JackTerrance
0 votes
    I have an issue with passing a struct definition to a function. Not an instance of a struct, but the definition. We ... (object sender, EventArgs e) { GetHeaders( MineStruct ); //...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have a PHP blade file and I have a list of components working as they but can't seem to pass ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Here is the story: My server is a cloud server running centos, and serves a few bunch of web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
...