top of page
Writer's pictureJatin Madaan

Functions in Python


This Post is about basics of functions in Python .


Functions are created so that we don't have to write same code repeatedly .


Syntax :

def function_name() :

function_body


## to call a function

function_name()


eg :



##Defining a function 
def python_name():
    print("Names are Name1 & Name2")
    
## Calling a function 
python_name()

## default return value of a function is None
print(python_name())

O/P :

Names are Name1 & Name2 Names are Name1 & Name2 None


Functions with arguments/Parameters .


Arguments/Parameters are the values which are passed to a function/method. It can be a text value or int etc .


eg :



## Below text is an argument to a function 
def centre_text(text):
    left_margin = (80 - len(text)) // 2
    print(" " * left_margin, text)
    
## function calling 
centre_text("Keep this in centre of screen")

O/P :

Keep this in centre of screen


Now if we pass integer to above function as an argument while calling then if would give us error stating that int has no function len .


eg :

centre_text(12)

TypeError: object of type 'int' has no len()


To solve this we need to explicitly convert the integer value to string value.


eg :



## Below text is an argument to a function 
def centre_text(text):
    text = str(text)
    left_margin = (80 - len(text)) // 2
    print(" " * left_margin, text)
    
## function calling 
centre_text(12) 

O/P :

12



Passing multiple number of arguments




## Below *args is way of telling function that it can 
## have multiple values * means variable no. of parameters
def centre_text(*args):
    text = ""
    for arg in args : 
        text += str(arg) + ""
    left_margin = (80 - len(text)) // 2
    print(" " * left_margin, text)
    
## function calling 
centre_text("First_Name" , "Second_Name")

O/P :

First_Name Second_Name


Passing default parameters in function



## Below *args is way of telling function that it can 
## have multiple values * means variable no. of parameters
def centre_text(*args,sep=' '):
    text = ""
    for arg in args : 
        text += str(arg) + sep
    left_margin = (80 - len(text)) // 2
    print(" " * left_margin, text)
    
## function calling 
centre_text("First_Name" ,"Second_Name")

## Function call with colon : as a seperator
centre_text("First_Name" ,"Second_Name",sep=':')

O/P :

First_Name Second_Name First_Name:Second_Name:




Using Files with functions for O/P



For using with files , we are making change in function instead of print just add return so that result can be returned back and used by other variables .


## In function at end we are returning value so 
## that it can stored and used for writing to files 
def centre_text(*args,sep=' '):
    text = ""
    for arg in args : 
        text += str(arg) + sep
    left_margin = (80 - len(text)) // 2
    return " " * left_margin + text

## Open a file and enter data from function value     
with open("centre_file.txt",'w') as centre_file :
    data = centre_text("First_Name" ,"Second_Name",sep=':')
    print(data,file=centre_file)

O/P :


$cat centre_file.txt

First_Name:Second_Name:



This was very basic introduction of using functions in python.


=========================END================================

16 views0 comments

Comentarios


bottom of page