Question

In: Computer Science

“Starting Out with Python”, Chapter 5, introduces the student to functions. Describe in your own words...

“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?

Solutions

Expert Solution

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:

  1. Built-in functions
  2. User-Defined functions
  3. Lambda functions (Anonymus functions)

- Now, let's understand the usage of function practically:

  • Suppose you want to write a program that calculates simple interest for the values that user entered. And you want to run this program until user enters -1. So here instead of writing the code again and again we can define a function that takes n, p, and r as an argument and return the simple interest for these values.
  • Syntax:   
def calculate_simple_interest (n, p, r):
    # Code to calculate simple interest

    return simple_interest
  • One simple example to use function would be that we want to write a program that calculate HCF of 2 numbers. Now if we want to make our program dynamic then we can define a function "calculate_HCF" which will calculate the HCF for given numbers.
  • Syntax:
def calculate_HCF(num1, num2):
    # Code to calculate HCF of 2 numbers

    return HCF
  • There are a lot of built in functions available that can ease our work. Like enumerate(), abs(), filter(), etc. Even the print() is an inbuilt function.

- 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:)


Related Solutions

3- a) List and describe IN YOUR OWN WORDS the five functions of a manager?    ...
3- a) List and describe IN YOUR OWN WORDS the five functions of a manager?     b) How are financial statements used by business?     c) Compare variable and fixed expenses. USE YOUR OWN WORDS.
In your own words, briefly describe the process and mechanism of RNAi -- starting with double-stranded...
In your own words, briefly describe the process and mechanism of RNAi -- starting with double-stranded RNA (dsRNA) and ending with the destruction of mRNA. Include in your answer the roles of ds-RNA, dicer, siRNA, RISC, slicer, and RdRP.
1. Describe in your own words the structure of DNA. 2. Describe in your own words...
1. Describe in your own words the structure of DNA. 2. Describe in your own words how DNA makes copies of itself. (I.e. Describe DNA replication.). Protein synthesis involves two processes, transcription and translation. Describe in your own words how each process occurs. Transcription Translation
Describe the ‘crowding out’ perspective on fiscal policy in your own words. In your opinion, what...
Describe the ‘crowding out’ perspective on fiscal policy in your own words. In your opinion, what are the strengths and weaknesses of this argument? Why might government spending create a larger multiplier effect when unemployment is high and have a smaller multiplier effect when the economy is at full employment?
In your own words and understanding, explain any five(5) functions of the epidermis in flowering plants
In your own words and understanding, explain any five(5) functions of the epidermis in flowering plants
In your own words, describe the physiologic functions of the following gastric secretions: hydrochloric acid, pepsin,...
In your own words, describe the physiologic functions of the following gastric secretions: hydrochloric acid, pepsin, gastric lipase, intrinsic factor. In your own words, describe the stages of gastric activity: cephalic, gastric, and intestinal.
In your own words, describe the physiologic functions of the following gastric secretions: hydrochloric acid, pepsin,...
In your own words, describe the physiologic functions of the following gastric secretions: hydrochloric acid, pepsin, gastric lipase, intrinsic factor. In your own words, describe the stages of gastric activity: cephalic, gastric, and intestinal.
Discuss in your own words the major characteristics of a Project. In your own words, describe...
Discuss in your own words the major characteristics of a Project. In your own words, describe the benefits of a structured Project approach.
Anwser in your own words Describe in your own words, what should be included in health...
Anwser in your own words Describe in your own words, what should be included in health record documentation best practices. Provide at least one example of how HIM's ensure these best practices are met.
Describe, in your own words, how branch functions make dissassembly more difficult. Your answer should briefly...
Describe, in your own words, how branch functions make dissassembly more difficult. Your answer should briefly describe how linear sweep and recursive traversal disassembly work, and how branch functions affect both of them.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT