f(x)=2x+1 # function denoted by f is given as 2x+1
# what is the output value of f if x=2
# ANSWER: f(2)=2*2+1=5
#In python, we define function using def keyword followed by function name and parantheses;
#ending with colon
#Example:
def f(x): # function definition in python with one parameter x
y=2*x+1 # body section or block of code
return y # out put section
print(f(2)) # prints the output of the function f for x=2
5
#def add(a,b): # a and b are two parameters
#result=a+b # the implementation section
#return result # output section
print(add(2,3)) # prints the result of the function add that takes 2 and 3 as arguments
5
def add(a,b,c): # a, b, and c are three parameters
result=a+b+c # the implementation section
return result # output section
print(add(2,3,7)) # give error -type error as there is missing 1 required arguments
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[5], line 1
----> 1 print(add(2,3,7))
NameError: name 'add' is not defined
print(add(2,3, 5))
10
r1=add(2,3,4) # assigned to variable
r2=add(5,4,6)
def multiply(a,b):
r=a*b
return r
#To call a function, use its name followed by the parantheses.
#example:add() execute the code with in the function
#we can pass arguments (inputs) to the function with in the parantheses
#example: add(2,3,4) # invoking function add with three arguments
print(), len,type, etc. # examples of built in function in python
def _add(a,b):
y=a+b
return y
```