Notes
Notes - notes.io |
set of instruction to complete a specific task
large program are divided into basic building blocks
NEED
reusability
u can use function when the same code is repeated again and again
the larger program are divided into small parts and the small parts are combined into a single program
it is easy for debugging ,testing and maintenance when program is divided
the length of the program is reduced
THERE ARE TWO TYPES OF FUNCTION
BUILTIN FUNCTION
USER DEFINED FUNCTION
1. built in function
the function are already created and stored in python
foe usage programmer can access it
2.userdefined function
programmer can create their own function for their requirement
and then you can combine the function into a module are use it in your program
FUNCTION DEFINITION
def keyword is used to define a function.
Give the function name after def keyword followed
by parentheses in which arguments are given.
End with colon (:)
Inside the function add the program statements to be
executed
End with or without return statement
FUNCTION CALLING
Once we have defined a function, we can call it from
another function, program or even the Python prompt.
To call a function we simply type the function name
with appropriate arguments.
FLOW OF EXCUTION
The order in which statements are executed is called the flow of execution
Execution always begins at the first statement of the program.
Statements are executed one at a time, in order, from top to bottom.
Function definitions do not alter the flow of execution of the program, but
remember that statements inside the function are not executed until the
function is called.
Function calls are like a bypass in the flow of execution.
Function Prototypes
i. Function without arguments and without return type
ii. Function with arguments and without return type
iii. Function without arguments and with return type
iv. Function with arguments and with return type
i)Function without arguments and without
return type
o In this type no argument is passed through the
function call and no output is return to main function
o The sub function will read the input values perform
the operation and print the result in the same block.iii) Function without arguments and with return
type
In this type no argument is passed through the
function call but output is return to the main
function.
iv) Function with arguments and with return type
In this type arguments are passed through
the function call and output is return to the
main function
ii)Function with arguments and without return
type
Arguments are passed through the function call
but output is Parameters
Parameters are the value(s) provided in the
parenthesis when we write function header.
These are the values required by function to work.
If there is more than one value required, all of them
will be listed in parameter list separated by comma.
Example: def my_add(a,b): not return to the main function
Arguments
·Arguments are the value(s) provided in function call/invoke
statement.
List of arguments should be supplied in same way as
parameters are listed.
ARGUMENTS TYPES
1. Required Arguments
2. Keyword Arguments
3. Default Arguments
4. Variable length Arguments
www.skcet.ac.in
www.skcet.ac.in
1. Required Arguments:
The number of arguments in the function call should match exactly with the function definition.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details("george",56)
Output:
Name: george
Age 56
2. Keyword Arguments:
Python interpreter is able to use the keywords provided to match the values with parameters
even though if they are arranged in out of order.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details(age=56,name="george")
Output:
Name: george
Age 56
www.skcet.ac.in
3. Default Arguments:
Assumes a default value if a value is not provided in the function call for that
argument.
def my_details( name, age=40 ):
print("Name: ", name)
print("Age ", age)
return
my_details(name="george")
Output:
Name: george
Age 40
4. Variable length Arguments
If we want to specify more arguments than specified while defining the function,
variable length arguments are used. It is denoted by * symbol before parameter.
def my_details(*name ):
print(*name)
my_details("rajan","rahul","micheal",
ärjun")
Output:
rajan rahul micheal ärjun
A module is a file containing Python definitions ,functions,
statements and instructions.
Standard library of Python is extended as modules.
To use these modules in a program, programmer needs to
import the module.
Once we import a module, we can reference or use to any of
its functions or variables in our code.
import module_name
module_name.function_name(variable)
Importing Builtin Module:
import math
x=math.sqrt(25)
print(x)
Importing User Defined Module:
import cal
x=cal.add(5,4)
print(x)
2.random- Generate pseudo-random numbers
random.randrange(stop)
random.randrange(start, stop[, step])
random.uniform(a, b)
-Return a random floating point number
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team