Why do we use def ()



Why do we use def() can someone explain me the working mechanism of def()

Replace this line with your code.


def() is the python syntax used to create functions and why functions are very important is because they make sure you don't have to write the same code over and over again because functions are reusable,

If I wanted to create a function that takes two parameters and adds them I would simply write,

def add(x, y): #Function definition using def() keyword
    return x + y #Function Body

print(add(192, 53)) #Function call

I have just created a function that can add two numbers and all I have to do now each time I would like to add two numbers is call my add function! :slight_smile:


functions are basically like variables but you can activate multiple codes without copy pasting everytime
Hope I explained it properly :smiley:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.