Add two numbers

To carry on my normal process of notes on this website for different languages here is the how to add two numbers with a return value from a function.

To define a function you use the “def” syntax within the programming language and then the next bit of text before the parameters for the function itself ( in this case the parameters are the (number1, number2) ), is the function name and how to call the function.

The function below takes two parameters and returns back there result of the mathematical addition, the last part is how to call the function.

def addTwo(number1, number2) : 
	return number1 + number2
 
print addTwo(1,2)

and the output would be

3

4 thoughts on “Add two numbers”

  1. Hi, I need to know the code for adding two numbers from getting them from two different text boxes and displaying them in another text box, could some one suggest me.. here is my code please check
    import wx
    import math

    class View(wx.Frame):
    def __init__(self):
    self.frame1=wx.Frame.__init__(self, None, wx.ID_ANY, title=’open1′,pos=(5,5),size=(850,150),style=wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.CAPTION|wx.CLOSE_BOX|wx.CLIP_CHILDREN)
    self.panel=wx.Panel(self,-1)
    self.Btnadd = wx.Button(self.panel, -1, “&Add”, pos=(665, 50),size=(80, 20))
    self.tb1 = wx.TextCtrl(self.panel,wx.ID_ANY,””, pos=(580,50),size=(50,20))
    self.tb2 = wx.TextCtrl(self.panel,wx.ID_ANY,””, pos=(580,80),size=(50,20))
    self.tb3 = wx.TextCtrl(self.panel,wx.ID_ANY,””, pos=(665,80),size=(60,20))
    self.Btnadd.Bind(wx.EVT_BUTTON,self.onaddvalue)

    def onaddvalue(self,event):
    self.tb1.GetValue()
    self.tb2.GetValue()
    sum= self.tb1Value+self.tb2Value
    self.tb3.SetValue(sum)
    if __name__ == ‘__main__’:
    app = wx.PySimpleApp()
    frame = View().Show()
    app.MainLoop()

  2. Hi, I need to know the code for adding two numbers from getting them from two different text boxes and displaying them in another text box when button add is clicked

  3. Hi Bala

    Is this for course work or just out of interest ? because I cannot help with course work.

    Regards
    Genux

Leave a Reply

Your email address will not be published. Required fields are marked *