This assignment involves writing some more functions in Python 👍
Last assignment aimed to strengthen your basic function writing skills while also learning the concept of typecasting to convert one data type to another.
This one will take half an hour, but will teach you all cool things about functions.
def printHello(name):
print "Hello" + name + "!"
printHello("Turtle")
def add(x, y):
sum = x+y
return sum
result = add(a, b)
-
Function Definiton - A header line which begins with a keyword and ends with a colon.
-
Function Declaration - A body consisting of one or more Python statements, each line should be indented the same amount.
-
Function call - Defining and Declating a new function does not make the function run. To do that we need a function call.
-
Parameters
- These are of two types
- Formal Parameters - The parameters/variables in the function definition are more specifically known as the formal parameters. Eg. def add(x, y): Here x and y are formal parameters.
- Actual Parameters - A function needs certain information to do its work. These values, often called arguments or actual parameters, are passed to the function by the user. They are in the function call, eg add(a,b) , a and b are actual parameters
- These are of two types
-
Return statement
- A function may or may not return anything
- A function that does our work but not returns anything, like this one,
def printHello(name): print "Hello" + name + "!" printHello("Turtle")
are called procedures or non - fruitful functions
- On the other hand, if it returns something, like
def add(x, y): sum = x+y return sum result = add(a, b)
then the function is called a fruitful function
- The return statement is followed by an expression which is evaluated. Its result is returned to the caller. Okay sweet!
a + b is an expression
return a + b
-
Two types of Functions
- User-defined, which users write.
- Built - in functions, example - print(), abs(), len() Okay!
-
Local Scope and Global Scope [dude this is important 😄 ]
-
Remember if you want to know what a built-in function does, just type help(function_name) in Python interpreter Eg - help(print) help(id) help(len) help(abs)
- Write a function which returns max of 2 numbers. We can use if, elif and else, right?
- Find the output - Concept of local and global variables
a = 60
def func(z):
print("z = ", z)
a = 2
print('Changed local a to', a)
func(a)
print('Global a is still', a)
- What is the use of id(), print() function in python? You know the interpreter trick!
- What's the output:
def cube(x):
return x * x * x
x = cube(3)
print x
- Lets find the output , again! 😃
def foo(k):
k[0] = 1
q = [0]
foo(q)
print(q)
- Try this, find output?
def foo(i, x=[]):
x.append(x.append(i))
return x
for i in range(3):
y = foo(i)
print(y)
-
The trick for solving 'find the output' questions is to NOT think how the program wants to you to think. DON'T proceed with any notion of how that program is supposed to work, find out how that program will exactly work.
-
Try doing dry run! Okay! 😄