Skip to main content

18.Python Functions

PYTHON FUNCTIONS 
    A function is typically referred as a block of code which allows you to pass data/parameters and return data as a result. Functions are really useful to utilise code in more than one section in a program. In other words, It increases re-usability of code and helps to removes redundancy. The body of the function must be a indented statements. To end the function body, the indent( a blank space) has to be ended. A function may also consists a return statement to returns data. 


Pass: 
    Like in loops statement,you can also use pass to avoid any errors since the function cannot be empty. 
Return: 
    The return statement is used in function to return a value or output of that function 
syntax 
    It is defined using the def keyword.
def function_name(parameters): 
    body (statements) 
Syntax:Pass 
def function_name():
    pass 
Program
def power(x): 
    return x**9 
a=power(2) 
print(a) 
Output 
512 
Calling a function 
    In order to execute the function,a function has to be called.A function can be called by its name by passing the required values as parameters.in short,the function name followed by parenthesis is the syntax used for calling a function,In the program below a=power(2),b=power(5),c=add(89) are the function calls where power(),add() are the function names with one argument. 
Syntax: 
function_name(value) 
Program
def power(x): 
    return x**9 
def add(x): 
    return x+5 
a=power(2) 
print(a) 
b=power(5) 
print(b) 
c=add(89) 
print(c) 
Output 
512 
1953125 
94 
     It is also possible to return more than one value but you can use only one return and also you must specify appropriate number of variables to store the values returned.
Program
def operations(a,b):
    multiply=a*b
    divide=a/b
    return multiply, divide
result1,result2=operations(23,12)
print(result1,result2)
Output 
276 1.9166666666666667
Passing Parameters in a function by reference 
    A reference of actual parameter is passed to the function.The advantage of it is that whenever the parameter value is modified the change reflects back in the function call. In Python, scalar values are passed by-value. Lists and other objects are passed by reference. 
Program
def fruits(flist): 
    print("the fruits are:",flist) 
    flist.append('Tomato') 
    print("The fruits are now:",flist) 
flist=['Apple','Banana','Mango'] 
print(flist) 
fruits(flist) 
Output 
['Apple', 'Banana', 'Mango'] 
the fruits are: ['Apple', 'Banana', 'Mango'] 
The fruits are now: ['Apple', 'Banana', 'Mango', 'Tomato'] 
Watch the video below to learn python in detail
Happy Learning!!



Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl