8 - Function 2
8 - Function 2
FBT0015
STRUCTURED ALGORITHM
& PROGRAMMING
Learning Outcomes
function formal
name parameters
The syntax for defining a function is:
function
header
function
body
(need to be indented)
return value
Defining a Function (cont..)
A function contains :
header
begins with the def keyword, followed by the function’s
name and parameters, and ends with a colon.
the variables in the function header are known as formal
parameters or simply parameters. A parameter is like a
placeholder. When a function is invoked, the caller will pass
a value to the parameter. This value is referred to as an
actual parameter or argument. Parameters are optional.
body includes a collection of statements that define what the
function does.
Functions may or may not return a value. If a function returns a
value, it is called a value-returning function.
#define function without parameter Function
def Welcome(): definition
print('Welcome to LalaLand')
largerint(3,4)#call function
How to call a function?
If the function returns a value, a call to that function is usually
treated as a value. It could be assigned to a variable.
For Example:
larger = largerint(3,4)
or display on screen as
print(largerint(3,4))
Calling a function (cont..)
def largerint(num1,num2):
if num1>num2: result=num1
else: result=num2
return result
def main():
i=5; j=2
k= largerint(i,j)
print('The larger number of ', i ,' and', j , ' is ', k)
main()
Output:
..Thinking Time..
#call functions
print (largerint(18,9))
largerint(18,9)
largeAns=largerint(18,9)
print ('inside largeAns is ', largeAns)
print (largervalue(10,5))
largervalue(10,5)
largeVal=largervalue(10,5)
print ('inside largeVal is ', largeVal)
Functions with or without Return Values
(cont..)
Output:
..Thinking Time..
Output:
Positional & Keyword Arguments
Keyword arguments
Mix arguments
..Thinking Time..
def nPrintln(*message):
print(message[3])
nPrintln ('sayang','saya','say','saying','sayung')
when you do not know how many arguments to get from your
caller, you may use Arbitrary Arguments (*message).
Passing Arguments by Reference
Values
When you invoke a function with arguments, the reference value of each
argument is passed to the parameter (pass-by-value).
If the argument is a number or a string, the argument is not affected,
regardless of the changes made to the parameter inside the function.
Output:
..Thinking Time..
Output:
Scope of Variables
Local Variable
variable created inside a function
can only be accessed within a function where its being created.
the scope of local variable starts from its creation and continues to the
end of the function that contains the variables.
a local variable could be bind in the global scope or a variable in a
function can be use outside the function by using a global statement
(refer scope of variable-Example 3)
Global variables
variable created outside all functions
accessible by all functions in their scope.
Scope of Variables (cont..)
Write a function that will find the smallest element from a list of integers
and returns the index of the smallest element found. Use the following
function header:
Write a test program that prompts the user to enter a list of number of
data type integer, invokes the function to return the index of the
smallest element, and displays the index.
Recursive function
product=1 5 - 1 -
product*=i 2 2 2
print (product) 3 6 6
4 24 24
Output:
Recursive function (cont..)
4 * factorial(3) 6 print(result) 24
3 * factorial(2) 2 print(result) 6
2 * factorial(1) 1 print(result) 2
factorial=1 print(result) 1
Recursive function (cont..)
Output:
Nested Function
Occur when another function declared in a function. Variable for inner function
can be used by itself only. Any variables from main program declaration and
outer functions can be used by inner functions.
Def functionOuter:
Def functionInner:
…..functionInnerBody……..
…..functionOuterBody……..
main
Nested Function (cont..)
Output:
Example:
Summary
A function header begins
Functions help in making
with the def keyword
program modular and A function is called a void or
followed by function’s
reusable, as that is one of None function if it does not
name and parameters, then
the central goals in software return a value.
ends with a colon.
engineering
Parameters are optional.