Lesson-7: Python Function
Python Function
Some of the functions used up to this section are as follows: print( ), input( ), int( ) and LEN( )
Function the same function as the functions in mathematics. defined a function such as y = f(x)=x*x multiplies the given value with itself. This is a relatively easy process. When it is necessary to calculate the square of a number, this function can be used instead of rewriting the formula. Functions are created to eliminate code rewrite, better organize codes, and then use these codes conveniently.
In Python and other programming languages, many functions come defined in various libraries or by default. In this way, even for the most basic operations, there is no need to repeat lines of code.
Each function has a name (print, input, len, etc.) and has structures called parameters that it can use to perform its function. Parameters specify properties and entries defined in the function. When using (calling) a function, the values given to these parameters are called “arguments”. Below is an example of using the print( ) function and parameters. ” end ” and ” sep ” are parameters. In functions, some parameters are mandatory and it is necessary to enter an argument (value). But again, some parameters are defined as a value/null (None) or selectively defined in the function. It is not necessary to value these parameters. These are optional, and there is no need to enter arguments for properties that will not be used.
Creating functions in Python
Basic functions are functions that are ready in Python. Since these functions are defined, they can only be used by typing functionName (). A function:
def funcName (parameters1, parameters2,..):
it is defined as. Codes in the function are written in the block structure as indentation. It is used by giving parameter values in the form of functionName(argum1, argum2) to use this function outside the function structure. For example, you can write a function that finds whether a number entered is an even number. The function will consist of a parameter and will take a number value.
Example-1: A function that writes to the screen whether the number is odd or even:
def funct (numb1):
if numb1%2==0:
print('number is even...')
else: ('number is odd...')
funct(10)
After creating a function, you can enter and call arguments appropriate to the parameters, as you use in the print( ) function. The function can be used by writing values to the name and parameters of the function. Performs operations according to the arguments given when the function is called. The function in the example checks the number 10 and prints the result on the screen as “number is even”.
Example-2: A function that prints as much text as the number entered on the screen:
def funct (text,howmany):
for i in range (1, (howmany+1)):
print (text, end='\n')
#Function calling..
funct('Hello Python', 5)
A defined function prints text as low as desired.
In this function, there will be two parameters: the text to be printed and the number of prints.
Examples-3: A function that finds whether a number is a prime number
def primefunct(numb):
counter=2 # since all numbers are divided by 1, we started with 2
while counter<=int(numb/2):
if numb%counter==0:
return False
counter+=1
return True
#Fonksiyonu çağırma
primefunct(113)
Screen Shot
True