Function-
● A function is a named block of code designed to perform a specific task.
● It takes in some input (optional), processes it, and gives us some output. (optional)
● A function once defined, can be called many times. eg: function to draw a square, function to calculate average, etc.
A function has 2 parts - function definition and function call.
Have you ever used a soft drink vending machine?
You give it some input (the drink you want, money ) and it does some processing inside(checks the amount, checks availability) and gives you the output(the soft drink).
Here, you are not bothered about, how the machine works or from where it gets the soft drink. right? You just give the input and you get the required output. isn’t it?
Functions in programming also behave in a similar way.
Function definition:
Set of instructions that form the function.
Syntax
def
Function Call:
Note: This function draws a square of length 150 units.
Note: The parameter is helping in customizing the code as per user need. Earlier squares of only predefined length could be drawn.
Write a function to draw a triangle and call it.
Modify the above program to draw a triangle of user-specified length.
Modify the above program to return the sum of the length of all 3 sides.
Draw a house of the user input length.
Note: The square is drawn downwards (turn right) and the triangle is drawn upwards. (turn left)
Comments
Post a Comment