Pass a value from a function to another function

pass variable from one function to another python
how to pass value from one $ function to another $ function in jquery
pass a variable from one function to another javascript
how to use one function variable in another function in javascript
how to use return value in another function python
how to use variable of one function in another function
how to get value from one function to another function in javascript
how to use one function variable in another function in python

I have a piece of code, disclaimer: I have not written a lot this code, others have helped. and i want to pass a parameter within it.

This is supposed to turn a binary number into a decimal, further converting the decimal to a binary.

If you look near the bottom, there's an input, taking 1111 as an example binary number, it turns into 15 as a decimal, which i want 15 to turn into a hexadecimal, not 1111.

How do i make it so that the second function, two() uses 15? I have a class and a constructor, i want to know how i can pass the end result of the function one() to the function two().

import os,time

class Helper:

    def __init__(self, num):
        self.num = num

    def one(self):
        b1 = self.num
        b2 = 0
        d1 = 0
        p = 0
        while(b1 != 0): 
            b2 = b1 % 10
            d1 = d1 + b2 * pow(2, p)
            b1 = b1//10
            p = p + 1
        print (d1)


    def two(self):
        n = self.num
        hex_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
        reversed_number = ""
        while n > 0:
            remainder = n % 16
            n -= remainder
            n //= 16
            reversed_number += str(hex_values[remainder])

        print(reversed_number[::-1])


os.system('clear')
print
print ("Input any number.")
n = Helper(int(input(">> ")))

time.sleep(1)
n.one()

time.sleep(1)
print
n.two()
print
time.sleep(1)

If you look at the function one() there is a print, i want to pass that value, not the variable b1 or the value of the input the user gave.

Here is s way to solve it

import os,time

class Helper:

    def __init__(self, num):
        self.num = num

    def one(self):
        b1 = self.num
        b2 = 0
        d1 = 0
        p = 0
        while(b1 != 0):
            b2 = b1 % 10
            d1 = d1 + b2 * pow(2, p)
            b1 = b1//10
            p = p + 1
        print (d1)
        return d1 # set the output of the function


    def two(self, n):
        hex_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
        reversed_number = ""
        while n > 0:
            remainder = n % 16
            n -= remainder
            n //= 16
            reversed_number += str(hex_values[remainder])

        print(reversed_number[::-1])


os.system('clear')
print
print ("Input any number.")
n = Helper(int(input(">> ")))

time.sleep(1)
decimal = n.one() # you define a variable as the output of n.one()

time.sleep(1)
print
n.two(decimal) # you pass the output of n.one() to n.two()
print
time.sleep(1)

You can set an output for your function n.one() using the return keyword the syntax is: return {value}, so if the value you want to return is d1 simply type

return d1

Use a variable to capture the output of n.one

decimal = n.one()

at this point to display the output of n.one() you could type

print(decimal)

instead of printing it in the function n.one() now make the function n.who() take the argument you passed

def two(self, n):

and delete n = self.num

finally you pass the output of n.one() to n.two()

n.two(decimal)

Hope this solved your issue

How can I pass a value from one function to another function without , (function(){ var a, c; // Not global, but available to all three functions function new1 (){ a = 5; } function new2(){ c = 6; } function new3(){ if(a < c)� Functions don’t necessarily need to consume variables. The graph() function from Passing a Value to a Function can gobble any int value, including an immediate value or a constant. Exercise 2: Edit the source code from Exercise 1, changing Line 13 so that the graph() function is passed a constant value of 64. Build and run.

your one method needs to return a value instead of printing it, so that you can save the value to a variable. Your two method then needs a parameter in order to pass the previous value.

def one(self):
    ...
    return d1

def two(self, value):
    ...

v = n.one()
n.two(v)

As an altenative, you can save the result of one method to an additional instance variable.

Chapter 15. Functions, You can call a function by mentioning its name, followed by arguments in parentheses: in the last example), passed as an argument to another function, and more. pass parameters by reference; that is, if you pass a variable to a function,� These functions evaluate mathematical expressions over a range of values. Typical function functions include integral, quad2d, fzero, and fminbnd. For example, to find the integral of the natural log from 0 through 5, pass a handle to the log function to integral.

You need to return the value instead of printing it.

def one( self ):
    [...]
    return d1

If you only need the output of one() as input to two() you can also skip the assignment to a variable. one() evaluates to a numeric value and you can directly pass it.

h = Helper( int(input( ">> " )) );
h.two( h.one() );

Or you make it part of the object, then you can just use it.

def one(self):
    [...]
    self.d1 = d1;

def two(self):
    value = self.d1;
    [...]

Pass Function to Another Function - MATLAB & Simulink, You can use function handles as input arguments to functions that evaluate mathematical expressions over a range of values, such as integral and fzero. Pass event handlers and other functions as props to child components: < button onClick = { this . handleClick } > If you need to have access to the parent component in the handler, you also need to bind the function to the component instance (see below).

Passing a function as an argument to another function in Python , One of the more hair-raising facts we learn in my introductory Python trainings is that you can pass functions into other functions. You can pass� As explained above, I need to pass "timestamp" to the other function i.e. I need to replace "pastDate.getTime()" with the value of "timestamp". – WebDevBooster Oct 14 '17 at 1:00

Passing function as an argument in Python, In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. To know more about� handleClick is a function, that accepts 1 argument called value. And when you invoke it once, it will return another function which can be used for the onClick React listener. In the example above you can see that I’m attaching the handleClick() method, and passing a string value so it may be printed in the browser console.

Passing functions as parameter of another function, var add = function (a, b) { return a+b; } var subtract = function (a, b) { return a-b; } var handle_data = function (func) { // get data from user or other external source� Example program – passing structure to function in C by value: In this program, the whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function.

Comments
  • I know this might seem like a repeat, there is a similar question to this, however it is in completely different context, and i'm not able to understand that very well ;)
  • The fundamental problem here is that your code simply prints stuff to the screen, and then basically forgets it. The function should return the value rather than just print it (and actually it would probably be a better design to only return and have the caller print or do whatever it wants with the returned value).
  • A number has one value, regardless of the base it's represented in. If you want different representation of the same number try: print("{0:d} {1:X} {2:b}".format(15, 15, 15)). Conversely, check docs.python.org/3/library/functions.html?highlight=int#int. So, the whole (end goal of the) exercise looks a bit like reinventing the wheel.
  • so i should do return d1?
  • Yeah, do that and take it from there. The rest of the code needs to be adapted correspondingly of course.
  • Uh, it still used the input as the num for the function two()
  • It works for me. Try to copy and paste to be sure you didn't miss anything
  • IT WORKED :D Thanks~! i realised i had an error in my code that's why it didnt work before!
  • It's not clear how this "doesn't appear to work"; probably you did something else or didn't understand exactly how to refactor your code to use this.'
  • did i mention i'm pretty new to python, it is most likely the latter
  • i can't exactly mark this correct as it hasn't worked for me, i'm gonna do more testing on this, wait please.