Informatics Practices · Ch 3 — Brief Overview of Python
Functions
Functions
When the same task has to be performed again and again in a program, writing the same statements repeatedly is wasteful and error-prone. The remedy is a function: a set of statements or instructions grouped together under a name, which performs a specified task. A function is defined once and can then be reused at any number of places in the program simply by writing its name — this is called calling the function.
Why functions — the compound interest example
Suppose a program needs to calculate compound interest at multiple places. Instead of writing out the interest calculation each time, we can create a function — say CalcCompInt — and inside it write the code that takes the inputs (such as interest rate, duration and principal), calculates the interest, and displays the output. Wherever compound interest is to be computed, we simply call the function by writing its name, CalcCompInt. The same code is reused, saving time and effort.
Built-in functions and modules
Python comes with many predefined functions, called built-in functions. Two of them are already familiar: print() and input(). Using built-in functions makes programming faster and more efficient, since the work of writing and testing that code has already been done.
Closely related is the idea of a module — a Python file in which multiple functions are grouped together. The functions of a module can be used in a program easily by importing the module with the import command.
What you must know to use a built-in function
To use any built-in function, three things about it must be known:
- Function name — the name by which the function is called.
- Arguments — while calling a function, we may pass value(s), called arguments, enclosed in parentheses. The function does its work based on these values. A function may or may not take arguments.
- Return value — a function may or may not return one or more values. It performs its operations on the basis of the arguments passed to it, and the result is passed back to the point from which it was called. Some functions return nothing at all.
Reading a real program
The following short program uses three built-in functions — input(), int() and print():
#Calculate square of a number
num = int(input("Enter the first number"))
square = num * num
print("the square of", num, " is ", square)
Observe how the three functions are at work:
- The first statement uses two built-in functions at once:
int()andinput(). The third line uses a third function,print(). - The
input()function accepts one argument — the prompt string "Enter the first number". An argument is precisely this: a value passed within the parentheses. - The
print()function in the last line has four arguments — "the square of",num, " is " andsquare— separated by commas. - The
int()function takes as its argument the value entered by the user from the keyboard (whichinput()supplies as a string) and returns it converted into an integer. Its return value is therefore an integer, which gets stored innum.
Commonly used built-in functions …
| Input/ Output | Datatype Conversion | Mathematical Functions | Other Functions |
|---|---|---|---|
| input() | bool() | abs() | import() |
| print() | chr() | divmod() | len() |
| dict() | max() | range() | |
| float() | min() | type() | |
| int() | pow() | ||
| list() | sum() | ||
| ord() |