In this tutorial we are going to learn :

  • Functions in Python
  • Arguments
  • Types of Functions

 

 

Functions in Python

 

In Python we used lots of functions such as print(), input(), lower() etc. Now we are going to create our functions.

  • Function is a block of code that only runs when it is called. In our programs, we can create our functions one time and we can use them over and over again whenever we need them.  So functions save us from code complexity.
  • To create functions we use the def keyword.
  • To call a function we use the function name with parentheses.

Example :

def board():    # defining function
    print("Hi ")
    print("Welcome to your board.")
    
    
board()     # calling function 

 

 

 

Arguments

 

We can pass information as arguments to use them in our program. We use parentheses for arguments.

Example :

def board(first_name):      # defining function
    print(f"Hi {first_name}")
    print("Welcome to your board.")


board("Adam")       # calling function 

 

 

 

Types Of Functions


We have two types of functions:

  • Doing a task
  • Returning a value


Example:

Returning a task is a better way for our programs. Because :

  • We can print it if we need
  • We can write the data to a text file or we can do any other complex jobs
# 1- Doing a task 
# 2- Returning a value

# 1- Doing a task 
def board(first_name):
    print(f"Hi {first_name}")
    print("Welcome to your board")
    

# 2- Returning a value 
def get_board(first_name):
    return f"Hi {first_name}"
    
    
your_message = get_board("James")
print(your_message) 
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments