coderz.py

Keep Coding Keep Cheering!

User-Defined Functions in Python

JOIN Clause

A function is a collection of statements that accept input, perform some specific computation, and return output. The idea is to combine some frequently or repeatedly performed tasks into a function so that instead of writing the same code for different inputs, we can call the function.

All functions written by the user fall into the category of user-defined functions. It contains the following components:

  • Begin the function definition with the def keyword.
  • Give your function a name.
  • Provide one or more parameters. Although parameters are optional, parentheses are not. Then comes a colon.
  • Enter lines of code that tell your function what to do. In the sample code below, enter the code where the # logic is shown.
  • To return the output, use the return keyword at the end of the function. This is also an option. If this parameter is not provided, the function will return None.

Syntax:

def function_name (parameter_one, parameter_two, parameter_n):
  # logic here
  return

Example 1: simple function

def fun():
    print("Inside function")
 fun()

Output:

Inside function

Example 2: parameterized function

def add(x, y):
    print(x+y)
 
add(3,9)

Output:

12

Example 3: default function

A default argument is a parameter that takes on a default value if no value is specified for it in the function call.

def add(x, y=10):
    print(x+y)
 
add(3,9)
add(3)

Output:

12
13
Return value:

A return statement terminates the function call’s execution and “returns” the result (the value of the expression following the return keyword) to the caller. The statements following the return statements are ignored. If there is no expression in the return statement, the special value None is returned. In order to make a function return a certain value, put an expression in front of the return statement. The value of this expression may be assigned to some variable for subsequent processing.
Syntax: 
 

def fun():
    #statements
    return [expression]

Example:

def add(a, b):
  
    # returning sum of a and b
    return a + b
ans = add(12, 39)
print("Result of add function is {}".format(ans))

Output:

Result of add function is 51
Aliasing function names:

An alias is the second name given to a piece of data in Python programming. Because variables are just names that store references to actual values, aliasing occurs when the value of one variable is assigned to another variable.

Example:

def add(a, b):
   
    # returning sum of a and b
    return a + b
xyz=add
ans = xyz(12, 39)
print("Result of add function is {}".format(ans))

Output:

Result of add function is 51

Note: also read about Loops in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement