[Python] Functions :)

 In python, you can create your own function. 

First you need to define the function; def 

def

give python specific names. 

def go_home(): # two or more function, you use underscore_
    print("I wanna get off")

we need function name (go_home) and parenthesis () and : 

To be considered inside of the function, it has to be indented     print("I wanna get off")

Now we call the function.. ;
go_home()

I wanna get off



One more I want to tell you.. ;
def go_home(name):
    print("Get home, " + name)

go_home("John")
go_home("Mary")


This time I added something inside of parenthesis, which means whenever you use go_home function, you have to give the names. 

Let's Run.
Get home, John
Get home, Mary




You can add more parameters 
def go_home(name, age):
    print("Get home, " + name + ", your number is " + age)

go_home("John", "30")
go_home("Mary", "35")


and Run again
Get home, John, your number is 30
Get home, Mary, your number is 35



if you want to pass "integer" instead of string for the number, you can convert "age number" into "str"
def go_home(name, age):
    print("Get home, " + name + ", your number is " + str(age))

go_home("John", 30)
go_home("Mary", 35)

Get home, John, your number is 30
Get home, Mary, your number is 35

Then you get the same  :) 

Popular posts from this blog

[Python] Dictionary

[Visual Design 2/3]

[JavaScript] For loop , Function