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

Functions Notes

This document discusses different types of functions in Python including: 1. Required arguments - Functions that require a specific number and type of arguments to be passed to them. 2. Keyword arguments - Functions that allow arguments to be passed by name instead of position. 3. Default arguments - Functions that set default values for optional arguments so they don't need to be passed if the default is desired. 4. Variable length arguments - Functions that allow a variable number of arguments to be passed via the *args syntax.

Uploaded by

pranavr12s22021
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Functions Notes

This document discusses different types of functions in Python including: 1. Required arguments - Functions that require a specific number and type of arguments to be passed to them. 2. Keyword arguments - Functions that allow arguments to be passed by name instead of position. 3. Default arguments - Functions that set default values for optional arguments so they don't need to be passed if the default is desired. 4. Variable length arguments - Functions that allow a variable number of arguments to be passed via the *args syntax.

Uploaded by

pranavr12s22021
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function definition

1. A group of related statements that perform a specific task.


2. It is a block of organised, reusable code.
3. It provides better modularity for your application and a high degree of code
reusing

def - The keyword ' def ' that marks the start of a function header.

function name - uniquely identifying the function

parameters - to pass values to a function. They are optional.

: - to mark the end of the function header

docstring - optional documentation string to describe what the function does.

return - optional statement. Used to exit from a function and go back to the
place from where it was called. Also used to return a value from a function

__doc__ - docstring is available to us through print statement through


__doc__ attribute of the function.

# Differences between comment and docstring

Use comments to explain how code works.


Comments are great for leaving notes for people working on your program.
Docstrings provide documentation about functions, classes, and modules.
Use docstrings to teach other developers how to use your program.
...................................................................................
...................

#function demo for greeting


def greet(n):
"This function performs greeting action" #docstring

#print("Hello " + n + ", Good Morning")


#return()

n1=input("Enter a name")
#s=greet(n1) #Function calling
print(greet(n1))
print(greet.__doc__) # giving out the documentation string at runtime

...................................................................................
................

#Function with two arguments


def score(name,totalscore):
#print("name: "+ name + "totalscore: " + totalscore)
print("%s scored %d" %(name,totalscore))
score('Ann',9.5) # Implicit function calling with position of arguments
score(totalscore=9,name='Amrita') #Explicit function calling with names

...................................................................................
......

#Function with one initialized argument


def area(r,pi=3.14):
a=pi*r*r
print("Area of the circle is: " , a)
area(10)# Calling the function with a single value (other is already initialised)

................................................................................

#if-else inside a function


def absolute(num):
if num>=0:
return num
else:
return -num
print(absolute(10))#Function Calling
print(absolute(-200))#Function Calling

...................................................................................
..
#Function with boolean return
def boolean(b):
return bool(b)

result=boolean(2>5)
print("The result is: ",result)

..............................................................................

# Returning Multiple values


def alphanum():
return 'x','t',100,200
print(alphanum()) # printing the result a tuple

a,b,c,d=alphanum() # Unpacking the result tuple into different variables

result=alphanum() #Unpacking the result tuple and printing the result using index
values
print(result[0])
print(result[1])
print(result[2])
print(result[3])

...................................................................................
.........
Function Types

#Required arguments
#Keyword Arguments
#Default Arguments
#Variable length Arguments
.....................................................
#Required arguments
def valuechange(a):
a=10
print("Inside, the value of a is ", a)

# Main Program Code


a=int(input("Enter a number"))
valuechange()#Fn call
print("Outside the fun - valu of a is", a)

...................................................................................
.........
#Keyword Arguments
def studinfo(rollno,name,course):
print("Roll NO : ",rollno)
print("Name : ",name)
print("Course : ",course)

#Fun call
studinfo(course="UG",rollno=50,name='John')

..............................................................................
#Default Arguments
def studinfo(rollno,name,course="UG"):
print("Roll NO : ",rollno)
print("Name : ",name)
print("Course : ",course)

#Fun call
studinfo(rollno=51,name='Jack')

......................................................................

#Variable length Arguments


def varlen(*arg):
print("Result:", )
for i in arg:
print(i)

varlen(100,35,87,5000) #Fn Call

You might also like