In: Computer Science
“Starting Out with Python”, Chapter 5, introduces the student to functions. Describe in your own words some ways functions are beneficial to the programmer. In other words, why might a programmer want to use functions?
Hey There,
- I understood your question and I gave my answer to the best of my knowledge.
- If you need further explanation then do let me know in the comments box. I will be more than happy to help you:)
Answer:
- Basically a Function is a group of statements that perform a specific task. We use functions in python to bundle a set of instructions that we want to use repeatedly.
- If we have a very long code then by using the concept of the function we can divide our whole code into smaller and modular chunks.
- If there is a case where we are making some kinda reputation in our code then instead of writing the code again and again we can define a function and then reuse it.
- Functions increases the readability of the code.
- There are 3 types of functions in python:
- Now, let's understand the usage of function practically:
def calculate_simple_interest (n, p, r):
# Code to calculate simple interest
return simple_interest
def calculate_HCF(num1, num2):
# Code to calculate HCF of 2 numbers
return HCF
- We can see that the function is very beneficial for the programmer as it increases readability, avoids repetition, encourages the idea of reusing, etc.
Hope it helps:)