Popular Posts

Wednesday, August 26, 2020

Python Beginner: Functions

 Hello, developer!


Today, we will learn the Functions. A function is a group of codes execute for purpose. You already used some functions previous posts using import command. You can divide your whole program codes into some parts as functions and you can effectively manage your program development cycle better. Function is one of import concept in programming so we wish you learn it as clearly.


Let's start to make function with following command.


Function definition and coding example

def function_name():

       print("This is my first Python function!") # In function code(This line is indented)


"def" is function definition command and next lines are automatically indented. If your codes are not indented, it should not be in function.


If you want to input any value in function, you can use "parameter" to input the value in the function as below codes.


def my_friend(name, age): # "name", "age" are parameters, we can input values in the function through parameters 

       print("Hello " + name + ", you are " + str(age)) # In function code(This line is indented)


my_friend("Mike", 35) # Out of function code(This line is not indented, "my_friend" function execution code)  

my_friend("Steve", 70) # Out of function code(This line is not indented, "my_friend" function execution code) 


I showed full exercise codes as following and execution result.

def function_name():
print("This is my first Python function!") # In function code(This line is indented)

print("Top")
function_name()
print("Bottom")


def my_friend(name, age): # "name", "age" are parameters, we can input values in the function through parameters
print("Hello " + name + ", you are " + str(age)) # In function code(This line is indented)

my_friend("Mike", 35) # Out of function code(This line is not indented, "my_friend" function execution code)
my_friend("Steve", 70) # Out of function code(This line is not indented, "my_friend" function execution code)


Can you see the same result? Congratulations! Now you are developer who can use function.

I recommend you how can you utilize this function as well.


Thank you and see you on next post! 😊








No comments:

Post a Comment

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...