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

  • Home
  • SEARCH
  • 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 7513489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:12:05+00:00 2026-05-30T00:12:05+00:00

As I muddle my way through trying to learn how to code (in python)

  • 0

As I muddle my way through trying to learn how to code (in python) I’ve hit the same problem I hit frequently.

How do you pass variables to and from functions properly.

In this example, I want to create a new variable inside the function process, but I don’t know how to get it back properly (this method does not work.)

a = "foo"
def func_1():
 b = "bar"
 print a,b
 return (b)
func_1()
print b 

I am ‘expecting’ b to be available after the function call, as I have returned it… I appreciate this me not understanding properly how to implement/manage variables

Thanks.

  • 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-30T00:12:07+00:00Added an answer on May 30, 2026 at 12:12 am

    I think this is what you want to do:

    a = "foo"
    def func_1():
     b = "bar"
     print a,b
     return b     # Took out parenthasis
    returned_b = func_1()  # Added variable to hold the returned "b"
    print returned_b 
    

    It looks like you are just learning to code in general. Your issues look to be with understanding how computer code typically works, not so much with Python in particular.

    A couple of other things.

    In the line a = "foo", a is a global variable. This is often frowned upon in programming because it can create confusion (although I feel like it may be used more in Python in particular).

    It looks like you are having trouble understanding an aspect of how variables work called ‘scope’. When you declared the variable b inside of func_1(), it had only function scope. Once the function exited, the variable no longer existed unless you returned the value AND stored it in another variable, that may, coincidentally, also have been called b.

    I’m not sure you will be able to learn these concepts out of a normal Python book geared towards people who already know how to program. What you probably need is a book that teaches you how to program in general, and also uses Python as the language of choice. As I mentioned in a comment up top, finding the right learning source is key, otherwise you can be in for a world of frustration.

    Here is an example of how a “normal” program might be set up:

    def main():                 # Calling main() "runs" the program
       a = add(1,2)
       print a
    
    def add(first, second):     # functions are normally declared outside of other functions, 
       result = first + second  #  that way, they can be used anywhere
       return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm moving on from iOS to Cocoa and trying to muddle through my first
... or perhaps a way to launch NetBeans through a middle-man app and open
Is there any easy way to create an acronym from a string? First_name Middle_name
I'm pretty rubbish with CSS, I muddle through and rather than bash my head
I'm new to android but I've been working my way through the tutoials. Still
Im trying to build a kd-tree for searching through a set of points, but
I am trying to using a SQL data reader to move records from one
Normally I love to keep my HTML code clean, semantic and free from either
I'm trying to port an iPhone application from using SQLite to Core Data. Is
A little background: I'm in the process of learning Python through O'Reilly's, Learning Python

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.