#!/usr/bin/env python # coding: utf-8 # # 1. Simple function # In[2]: def foo(): print("This is foo") foo() # # 2. Function with parameters # In[3]: def bar(x, y): print("Sum is ", x + y) bar(3, 5) # # 3. Default parameter value # In[5]: def drawLine(length = 10): str = "" for i in range(0, length): str += "_" print(str) drawLine() drawLine(40) drawLine(length=20) # # 4. Lambda expressions # In[6]: def greeter(val): return lambda x: val + x greet = greeter("Hello ") print(greet("Alice!")) print(greet("Bob!")) # # 5. Recursive functions # In[1]: def factorial(x): if x == 1: return 1 else: return (x * factorial(x-1)) x = 4 print("The factorial of", x, "is", factorial(x))