Lecture 13 Functions
Lecture 13 Functions
nrfact=1
nr=n-r
for i in range(1,nr+1):
nrfact*=i
P=nfact/nrfact
rfact=1
for i in range(1,r+1):
rfact*=i
C=nfact/(rfact *nrfact)
• Function definition is required when you define a function in one source file and you
call that function in another file. In such case you should define the function at the
top of the file calling the function
Syntax of Functions
def my_function():
Name function.
Code that represents the function
MORE about Functions
Function names have to follow the same rules as variable names.
def my_function():
hi()
Function with Parameters
def hi(name):
print(“Hello “ + name + “! How are you today?”)
Now you can say hi to John, James, and Maggie, like so:
hi(“John”)
hi(“James”)
hi(“Maggie”)
Functions With Default Values
def hi(name=”sir”):
print(“Hello “ + name + “! How are you today?”)
hi()
hi(“James”)
Return Values
Method2:
def sum(a, b):
c = a+b
print ("Sum of",+a,"and",+b,"is:",+c)
Calling a Function
• While creating a function, you give a definition of what the function has
to do.
• To use a function, you have to call that function to perform the defined task.
• When a program calls a function, program control is transferred to
the called function.
– A called function performs defined task and when its return statement is executed
or when its end of function block (indented statements) is reached, it returns
program control back to the main program.
• To call a function, you simply need to pass the required parameters
along with function name, and if function returns a value, then you can
store returned value.
Function Mechanism
• C program does not execute the statements in a function until the function is
called.
• When it is called, the program can send information to the function in the form
of one or more arguments although it is not a mandatory.
• Argument is a program data needed by the function to perform its task.
• When the function finished processing, program returns to the same location
which called the function.
Function Mechanism
▪ Function can be called as many times as needed in a
program.
✓ Example: We can call add (int, int) function 10, 20, even 100
times if we need it.
▪ Functions can be called in any order provided that they
have been declared (as a prototype) and defined.
▪ Function can be called within any other function, but it can
not be defined inside any function.
Definition
Example
Function Call
Example
def add():
num1, num2 = 5, 15
num3 = num1 + num2
print(f"The addition of {num1} and {num2} results {ans}.")
add()
Function Parameters
• We might have functions that need some arguments to work
on.
– When the function is defined, the arguments are stored in placeholder
variables called formal parameters.
– The formal parameters behave like other local variables inside the function
and are created upon entry into the function and destroyed upon exit.
Example of Function Parameters
• Looking at the add function
• The values that are passed to the function when the function
is called are known as function arguments.
Passing Arguments to a Function
▪ In order function to interact with another functions or codes, the
function passes arguments.
▪ The called function receives the values passed to it and stores them
in its parameters.
▪ List them in parentheses following the function name.
▪ The number of arguments and the type of each argument must
match the parameters in the function header and call.
Passing Arguments to a Function
▪ If the function takes multiple arguments, the arguments listed in the function
call are assigned to the function parameters in order.
▪ The first argument to the first parameter, the second argument to the second
parameter and so on as illustrated below.
Definition
(Parameters)
def
Function Call
(Arguments)
FUNCTIONS
Do not pass argument Do pass arguments
No return
With a return
Parameters vs Arguments
• Parameter: a declaration of an identifier within the '()' of a
function declaration.
✓Used within the body of the function as a variable of that function.
Introduction to Functions 32
Using Functions (continued)
This is a parameter
• Let def func( x, a): be a function.
N = func(pi*pow(r,2), b+c) + d;
This is an argument
Introduction to Functions 33
Using Functions (continued)
• Let def func(x, a) be (the beginning of) a declaration of a
function.
def nPr(n,r):
P = factorial(n)/factorial(n-r)
print(f"The value of nPr for n={n} and r={r} is {P}")
def nCr(n,r):
C = factorial(n)/(factorial(r)* factorial(n-r))
print(f"The value of nCr for n={n} and r={r} is {C}")
Data Sharing Global Variables Offers Data Sharing Local Variables don't offer Data Sharing
For global variables, parameter passing is For local variables, parameter passing is
Parameter Passing not necessary necessary
Changes in a global variable are reflected Changes in a local variable don't affect
Changes in a variable value throughout the code other functions of the program
Example (Global Variables)
Example (global Variables)
a=10
def func():
global a
a+=10
print("Value inside the function:",a)
func()
print("Value Outside the function:",a)
Formal Parameters
• Function parameters (formal parameters) are treated as local variables
with-in that function.
See:
https://stackoverflow.com/questions/2612802/
list-changes-unexpectedly-after-assignment-
Copy In List
Lambda Function