0% found this document useful (0 votes)
19 views22 pages

Working With: Chapter - 3

This document discusses functions in Python. It defines a function as a subprogram that acts on data and returns values. Functions make programs easier to manage by breaking large programs into smaller units. The document covers the advantages of using functions, different types of functions in Python including built-in functions, module functions, and user-defined functions. It provides examples of defining and calling functions in Python.

Uploaded by

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

Working With: Chapter - 3

This document discusses functions in Python. It defines a function as a subprogram that acts on data and returns values. Functions make programs easier to manage by breaking large programs into smaller units. The document covers the advantages of using functions, different types of functions in Python including built-in functions, module functions, and user-defined functions. It provides examples of defining and calling functions in Python.

Uploaded by

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

CHAPTER - 3

WORKING
WITH
FUNCTIONS
Functions :
 Large programs are avoided because it is difficult
to manage a single list of instructions.
 Thus, a large program is broken into smaller units
known as functions.
 A function is a subprogram that acts on data and
returns values.
Advantages of using functions :
- It makes program handling easier as a small part of
program is dealt at a time.
- It reduces size of program.
- It makes program more readable and understandable.
- Same piece of code can be invoked many times.
Functions in :
Mathematics :
f(x) = 2x where f, x is its argument
i.e., value given to it.
and 2x is functionality.
For different values of argument x, function f(x) will
return different results.
Python :

def means function is starting (keyword)


function name is f
x is argument to function
colon ( : ) means it requires block
indentation defines working of function
return statement returns the calculated value
Python Function
Types
1. Built-in-functions
These are pre-defined functions and are always
available for use.
e.g : len(), type(), int(), input(), print()
2. Functions defined in modules
These functions are defined in particular module
or library which is only used when corresponding
library or module is imported.

e.g : To use fact(), sin() function we need to


import math module.
3. User defined functions

These are defined by programmers.


We can create our own functions.
DEFINING FUNCTIONS
IN PYTHON
SYNTAX or FORMAT :

def <function name> ( [parameters] ) :


[ “ “ “ < function's docstring> “ “ “ ]
<statement>
[<statement>]
.
.
.
Program to add two numbers
through a function.
While executing Programs
Python follows these guidelines:
1) Execution always begins with the first
statement of program.

2) Comment lines (lines starting with a #) ignored.

3) If python notices that it is a function definition (def


statements) then python just executes the function
header line to determine it is a proper function
header and skips all lines in the function body.
4) The statements inside function body is not executed
untill the function is called.

5) When a code line contains a function call, Python


first jumps to the function header line and then to the
first line of the function body and starts executing it.

6) A function ends with a return statement or the last


statement of function body, whichever occurs earlier.
7) If the called function returns a value i.e., has a
statement like return <variable/value/expression> then
control will jump back to the function call statement
and completes it.

8) If the called function does not return any value i.e., the
return statement has no variable, value, expression, then
the control jumps back to the line following the function
call statement.
Arguments
and
Parameters
Parameters


Arguments
So we can say that :
 Values being passed through function call are called
as Arguments.
(or Actual Parameters or actual argument).

 Values being recieved in function definition are


called as Parameters.
(or formal parameters or formal arguments).
 So, Arguments appear in function call statement and
Parameters appear in function header.
 Arguments in Python can be of these value types:
1. Literals
2. variables
3. expression

 But the Parameters in Python have to be some names


i.e., variables to hold incoming values.
Valid function call statements
Error !! because a
function header can not
have expression.
LIKE
SHARE

SUBSCRIBE
Share it with your friends so that they too can know about this.

You might also like