Working with Functions – Python --- tutorial_1
Introduction:
Large programs are generally avoided because it is difficult to manage. Thus, a large program is broken down into
smaller units known as functions. A function is a named unit of group of program instructions/statements. This unit
can be invoked from other parts of the program.
A function is a programming block of codes which is used to perform a single, related task. It only runs when it is
called. We can pass data, known as parameters/arguments, into a function. A function can return data as a result.
We have already used some python built-in-functions like print(), etc. But we can also create our own functions.
These functions are called user-defined functions.
Advantages of Using functions:
1. Program development made easy and fast: Work can be divided among project members thus implementation
can be completed fast.
2. Program testing becomes easy: Easy to locate and isolate a faulty function for further investigation
3. Code sharing becomes possible: A function may be used later by many other programs this means that a python
programmer can use function written by others.
4. Code re-usability increases: A function can be used to keep a way from rewriting the same block of codes which
we are going use two or more locations in a program. This is especially useful if the code involved is long or
complicated.
5. Increases program readability: The length of the source program can be reduced by using/calling functions at
appropriate places so program becomes more readable.
6. Function facilitates procedural abstraction: Once a function is written, programmer would have to know to
invoke a function only, not it’s coding.
7. Functions facilitate the factoring of code: A function can be called in other function and so on…
Python Function Types:
1. Built-in functions:- these are pre-defined functions and are always available for use.
Example:- print( ), int( ), float( ), input( ) etc.
2. Functions defined in modules :- these functions are pre-defined in particular modules and can only be used when
the corresponding module is imported into your program.
Example:- if you want to use the function sin( ), you need to import the module math (that contain the definition
of the sin( )) in your program.
3. User defined functions:- these are defined by the programmer.
Example:- any function you created by the programmer (while naming the function you should follow the naming
rules of identifier in Python)
In this material we mainly learn about the user-defined functions.
But before discussing user-defined functions, I want to show you some examples of program where we used the
pre-defined function concept:-
Example:-
Example:-
Example:-
So, from the above example, you have to focus on two things while creating – functions:-
(i) Function definition: - i.e. what that function does?
(ii) Calling/invoking the function:- call the function from anywhere in your program where it requires (using the
function name as myfun( ) in the above example).
Defining/creating functions in Python:-
General format:-
def function_name(parameters):
statements
statements
statements
The first line of function definition that begins with keyword def and ends with a colon (:) , specifies the name of
the function and its parameters ---- that whole line is called Function header.
For example, consider some function definitions given below:-
Example:- Function definition without parameters and returning values:
def myfun( ):
print(“welcome to python”)
Example:- Function definition with parameters but not returning values:
def myfun(x, y):
s=x+y
print(“Addition = ”, s)
Example:- Function definition with parameters and returning values:
def myfun(x, y):
s=x+y
return s
Now, the conclusion is that everything is depends on the programmer; means when arguments/parameters are
required and when returning value is required…..all depends on the programmer.
So, first you think about ….”What you want to implement with the function?”………it’s very important…..then
only you can decide about arguments/ parameters and returning values to/from a function.
Now, to clear the situation, look at the following discussion:
Example:- program with function:-
Figure: to illustrate the control flow of your program while function call:-
when your program encounter myfun( ) means it’s a function call statement….then the program control passes to
the function….and executes all the parts of the function then after completing all tasks in function, the control
comes back to the next line of your actual program….and so on.
Now, the situations you may consider while writing program:-
Consider the program of addition of two numbers in three ways: (Just for building your clear concept
regarding….when arguments are required……………..when returning value is necessary ………
Program : Addition of any two given numbers.
1st situation: everything is done within function (so no arguments/parameters
are required and not returning values)
Output:-
Program : Addition of any two given numbers.
2nd situation: The two numbers is taken in actual part of the program; and in
function part- addition is calculated and result is displayed.
(in that situation – arguments/parameters are required…..but returning values
not required)
Output:-
Look at the above code carefully:
add(a,b) in function call statement
def add(x,y) in function header statement
a, b are two variables of actual program part called local variables and the values of a and b passes to the function;
In function definition the values a and b are received in variable x and y; then addition result is stored in variable s
(so, s is also a local variable of function part).
If you use the name a and b instead of x and y; then also your program works fine……….but doesn’t
confuse….about a and b in actual program part ………… and……….a and b in function part. (Both are different
variable).
add(a, b) in function call statement
Here a, b are different
def add(a, b) in function header statement variables in function part
and actual program part
( Local variable, global variable concept, I will discuss on next material ).
So, the following program also works fine……..
Program : Addition of any two given numbers.
3rd situation: The two numbers is taken in actual part of the program; and in
function part- addition is calculated but the result should be displayed in actual
program part.
(in that situation – arguments/parameters are required…..and returning value
is also required)
Output:-
Ok……………………
So, arguments/parameters, returning value all’s are in programmer hand.
Think about those things before writing the program….then your program works
fine………………
Program tasks:- using function concept:-
1. Calculator (addition, subtraction, multiplication, division) - two given numbers
2. Find factorial of a number
3. Check a given number is prime or not.
4. Print the series of odd numbers upto n
5. Print the table of any given number
you can practice any program on your choice.