0% found this document useful (0 votes)
33 views

17 Working with Functions Part 1

The document explains the concept of functions in programming, detailing their definition, structure, and types in Python. It covers the importance of functions for code readability, the distinction between arguments and parameters, and the various types of arguments such as positional, default, and keyword arguments. Additionally, it provides examples of function definitions and calls, illustrating how to implement and utilize functions effectively.

Uploaded by

aryansuthar194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

17 Working with Functions Part 1

The document explains the concept of functions in programming, detailing their definition, structure, and types in Python. It covers the importance of functions for code readability, the distinction between arguments and parameters, and the various types of arguments such as positional, default, and keyword arguments. Additionally, it provides examples of function definitions and calls, illustrating how to implement and utilize functions effectively.

Uploaded by

aryansuthar194
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Working with Functions

By Chirag Parikh
Introduction

 A large program is broken down into smaller units known as functions.

 A function is a named unit of a group of program statements that


performs a specific task.

 Functions make a program more readable and understandable.

By Chirag Parikh
Understanding Functions

 Function f(x) = 2x2 can be written in Python like:


def calcSomething( x ):
r = 2 * x **2
return r

 def means a function definition is starting

 Identifier following ‘def’ is the name of the function, i.e., here the
function name is calcSomething

By Chirag Parikh
Cont. Understanding Functions

 The variables/identifiers inside the parentheses are the arguments or


parameters, i.e., here x is the argument to function calcSomething

 There is a colon at the end of def line, meaning it requires a block

 The statements indented below the function, (i.e., block below def
line) defines the functionality of the function. This block is also called
body of the function.

By Chirag Parikh
Cont. Understanding Functions

 The return statements returns the computed result.

 To use a function that has been defined, we need to write a function


call statement. The general form is : <function-name>(<arguments>)

 E.g., calcSomething(5)

 Another function: def cube(x):


res = x ** 3
return res
By Chirag Parikh
Python function Types

1. Built-in Functions
These are pre-defined functions and are always available for use. E.g., len(
), type( ), int( ), 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.
3. User defined functions
These are defined by the programmer.

By Chirag Parikh
Top-level statements

 The statements which are not part of any functions, are not indented at all
and are called top-level statements.
 The top level statements are part of the main program. Internally Python
gives a special name to top-level statements as __main__.
 Python stores this name in a built-in variable called __name__.

By Chirag Parikh
Program to add two numbers through a function

By Chirag Parikh
Arguments and Parameters

 arguments: Python refers to the values being passed in function call, as


arguments. Alternative names are actual argument / actual parameter
 parameters: values being received in function header (function definition),
are known as parameters. Alternative names are formal parameter / formal
argument.
 Following are some valid function call statements:
multiply ( 3, 4)
p=9
multiply ( p, 5)
multiply ( p, p + 1)

By Chirag Parikh
Cont. Arguments and Parameters

 But a function header like the one shown below is invalid


def multiply ( a + 1, b):
.
.
 Error!! A function header cannot have expressions.

 If we are passing values of immutable types (e.g., numbers, strings) to the


called function then the called function cannot alter the values of passed
arguments. (Call By Value)

 But if we are passing the values of mutable types (e.g., list or dictionaries)
then called function would be able to make changes in them. (Call By
Reference)

By Chirag Parikh
Types of Arguments

 Python supports three types of formal arguments / parameters:

1. Positional arguments (Required Arguments)

2. Default arguments

3. Keyword (or named) arguments

By Chirag Parikh
Default Arguments

 Python allows us to assign default value(s) to a function’s parameter(s) which


is useful in case a matching argument is not passed in the function call
statement.

 The default values are specified in the function header of function definition.

 E.g., def interest (principal, time, rate = 0.10) :

 Valid function calls are:


si_int = interest (5400, 2)
si_int = interest (6100, 3, 0.15)
By Chirag Parikh
Cont. Default Arguments

 A parameter having a default value in function header becomes optional in


function call may or may not have value for it.

 Note: Non-default arguments cannot follow default arguments.

 E.g.
def interest (prin, time = 2, rate) : is invalid
def interest (prin, time = 2, rate = 0.10) : is valid

By Chirag Parikh
Keyword (Named) Arguments

 Python offers a way of writing function calls where you can write any
argument in any order provided you name the arguments when calling the
function.

 E.g.,
interest (time = 4, prin = 2600, rate = 0.09)
interest (time = 2, rate = 0.12, prin = 2000)

 This way of specifying names for the values being passed, in the function call
is known as keyword arguments.

By Chirag Parikh
Using multiple argument types together

 An argument list must first contain positional arguments followed by any


keyword argument.

 Having a positional arguments after keyword arguments will result into error.

 You cannot specify a value for an argument more than once.

 For instance, consider following function header:


def interest (prin, cc, time = 2, rate = 0.09) :
return prin * time * rate
By Chirag Parikh
Cont. Using multiple argument types
together
 Now, for above function, consider following call statements:
interest (prin = 3000, cc = 5) # legal
interest (rate = 0.12, prin = 5000, cc = 4) # legal
interest (cc = 4, rate = 0.12, prin = 5000) # legal
interest (5000, 3, rate = 0.05) #legal
interest (rate = 0.05, 5000, 3 ) # illegal
interest (5000, prin = 300, cc = 2) # illegal
interest (5000, principal = 300, cc = 2) # illegal
interest (500, time = 2, rate = 0.05) # illegal

By Chirag Parikh
Program
def interest(principal, time=2, rate=0.10):
return principal * rate * time
prin = float(input("Enter principal amount:"))
print("Simple interest with default ROI and time value is: ")
sil = interest(prin)
print("Rs. ",sil)
roi = float(input("Enter rate of interest (ROI):"))
time = int(input("Enter time in years:"))
print ("Simple interest with your provided ROI and time value is: ")
si2 = interest(prin,time, roi/100) Output:
print("Rs.",si2) Enter principal amount:5000
Simple interest with default ROI and time value is:
Rs. 1000.0
Enter rate of interest (ROI):5
Enter time in years:3
Simple interest with your provided ROI and time value is:
By Chirag Parikh
Rs. 750.0

You might also like