Why do we use def ()


#1



https://www.codecademy.com/courses/python-beginner-BxUFN/3/4?curriculum_id=4f89dab3d788890003000096


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


Replace this line with your code.


#2

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:


#3

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


#4

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