Functions and why use them

History of functional programming

Example of function

code

def my_function(fname):  print(fname + " Refsnes")my_function("Emil")my_function("Tobias")my_function("Linus")

output

Advantages using of functions

Disadvantages of using functions

Different types of function in Python

Built-in functions:

User-Defined Functions (UDFs):

Example of a user-defined function in Python

Code

def compute_square(num_to_square):    return num_to_square * num_to_squarenum_squared = compute_square(7)print('7 squared is', num_squared)

output

Types of user-defined functions

Function with no arguments and no return value:

# Python Function with No Arguments, and No Return Valuedef Adding():    a = 20    b = 30    Sum = a + b    print("After Calling the Function:", Sum)Adding()

Function with no arguments and a return value:

# Python Function with No Arguments, and with Return Valuedef Multiplication():    a = 10    b = 25    Multi = a * b    return Multiprint("multiplication is : ", Multiplication())

Function with arguments and no return value:

# Python Function with Arguments, and NO Return Valuedef Multiplications(a, b):    Multi = a * b    print("After Calling the Function:", Multi)Multiplications(10, 20)

Function with arguments and a return value:

# Python Function with Arguments, and  Return Valuedef Addition(a, b):    Sum = a + b    return Sum# We are calling the Function Outside the Function Definitionprint("After Calling the Function:", Addition(25, 45))

Why do we use user-defined functions?

- They allow modular programming.

- They allow faster execution.

- They can reduce network traffic.

- Reduction in Program Size:

- Reducing Complexity of Program:

- Easy to Debug and Maintain:

- Readability of Program:

- Code Reusability:

Is functional programming the future’s best coding paradigm

Take away

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store