Function .ps
[Pages:9]The Islamic University of Gaza Faculty of Engineering Dept. of Computer Engineering Intro. To Computers (LNGG 1003)
Lab 6
Function
Eng. Ibraheem Lubbad
April 7, 2017
Functions allow us to group a number of statements into a block of cade to organized reusable code that is used to perform a single, related action. Types of Functions
1. Built-in functions - Functions that are built into Python for example len() , sort() . 2. User-defined functions - Functions defined by the users themselves.
Creating a function:
In general, a function has the following syntax: Syntax of function
def function_name( parameters ): return [expression]
Example 1: def showGreeting():
print "hello python" return
Calling a function:
To use a function, you have to call or invoke it. Here we call the function showGreeting ()
Example 1 showGreeting()
Passing Parameters by Values
Example 2
def showGreeting ( name ): print "hello ",name return
One parameter (input)
To use greeting, function you have to call the function as greeting (input) Example 2 showGreeting ("ibraheem")
Example 2 def showGreeting ( name ):
print "hello ",name return
If you use function without pass parameter will get error.
Example 2 showGreeting ()
Default value:
A default value is value that assumes if a value is not provided in the function call for that argument.
Example 1 def showGreeting( name="ibraheem" ):
print "hello " , name return In this case we not pass value to, so name parameter will use a default value. Example 1 showGreeting()
If we pass value to "name" parameter, then will print a new value Example 1 showGreeting ("ali")
The return Statement
The return statement is used to exit a function and go back to the place from where it was called. A return statement with no parameter is the same as return None. All the above examples are not returning any value. You can return a value from a function as follows:
Example: Write a function to return sum two numbers:
Example 1 def sum_func( num1,num2 ):
x=num1+num2
return x
Use sum_fun funcrion print " result sum 5,9 = ",sum_func(5,9) print " result sum 35,5 = ",sum_func(35,5) print " result sum 10,8 = ",sum_func(10,8)
Scope of variables:
Parameters and variables defined inside a function is not visible from outside. So, they have a local scope. The lifetime of variables (exist in the memory) inside a function is as long as the function executes. They are destroyed (exit from the memory) once we return from the function.
local outside
Example
def my_func(): x = 10 print"Value of x inside function:", x print"Value of y inside function:", y
#-----------------------------------------------------x = 20 y=10 my_func() print "Value of x outside function:", x
On the other hand, variables outside of the function are visible from inside. They have a global scope.
Global statement:
We can use the global statement, the variable will become available "outside" the scope of the function.
Example
def my_func(): x = 10 global name name="ibraheem" print"Value of x inside function:", x print"Value of y inside function:", y
#-----------------------------------------------------x = 20 y=10 my_func() print "Value of x outside function:", x print "name ", name
Lab Work:
Q 1) Write function to compute the length of sequence (String, list ,Tupe ) as len() function :
def my_len(seq): count = 0; for i in seq: count=count +1
return count;
Solution
Use my_len function
items=["orange","apple","banana",5,8] x=my_len(items) print "length of sequence = ", x
Q 2) Write a python function to return sum numbers between x to y. where y > x, otherwise return -1.
Solution
def sum_range(x,y): if x> y : return -1 s=0 for i in range(x, y+1 ): s= s+ i return s
We can use sum_range function to sum numbers between 0 -10 , 1-100 and 10-50.
Use sum_range function
a= sum_range(0,10) b= sum_range(1,100) c= sum_range(20,5) print "sum numbers between 0 - 10 = ", a print "sum numbers between 1 - 100 = ", b print "sum numbers between 20- 5 = ", c
Once we have defined a function, we can call it from another function
Q 3) Write a function drawShape that use the following functions to display rectangular shape of stars. Function will take a width and height as parameter.
def addStars(times): for i in range(times): print "*",
def newLine(): print ""
Solution def drawShape(height ,width):
for i in range (height): addStars(width) newLine()
drawShape(4,6)
Use call drawShape function
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.