What is a Function ? Functions in Python (Complete Beginner Guide) part .1


✨ Introduction


In programming, we often need to perform the same task again and again. Writing the same code repeatedly can make programs long and confusing. It also creates different type of writing or logical error. It makes the program shorter without it the program may become longer or tough to debug.

This is where functions help us.

A function is one of the most important concepts in programming, especially in languages like Python.



๐Ÿ”น What is a Function?

A function is a block of code that performs a specific task. These are basically used to reuse or repeat a block of code again and again in a very long program. They are written by the program writer. Some functions are user-defined and some are built-in functions.

๐Ÿ‘‰ In simple words:

A function is like a machine. You give it input, it processes it, and gives you output.

✔ Key Points:

1.Every function has a unique name

2.It performs a specific task

3.It can be called (used) anytime


๐ŸŽฏ Why Do We Use Functions?

Functions make programming easier and more efficient.

✔ 1. Ease of Use

Write a code once and use it many times in a very long program.

✔ 2. Modularity

Break a large program into smaller parts to make convenience for writer

✔ 3. Reusability

Reuse the same function in different places again and again when needed.

✔ 4. Maintainability

Create an easiness to fix and update code more efficiently.

✔ 5. Readability

Code becomes clean and easy to understand for both reader and writer .


๐Ÿ”„ Return in Functions

A function can send back a value using the return keyword.

๐Ÿ‘‰ Example:

                  return a + b

 ✔ Some functions return values

 ✔ Some only display output (using print)


๐Ÿ”น Types of Functions

1. Built-in Functions

These are already created in Python. It means they are defined by the language writer. These are not redefined by the user that is why these are called the built-in-functions. The are created to perform some specific tasks in the program.

✔ Features:

1.Pre-defined

2.Ready to use

3.Perform common tasks

๐Ÿ‘‰ Example:

                   print()


2. User-Defined Functions

These are functions created by the programmer. It means these types of function defined by the user or program writer. The user creates it according to his needs. They are defined in the language for different purposes.

✔ Features:

1.Custom-made

2.Designed for specific tasks


๐Ÿงพ Syntax of a Function

The syntax of writing a function in Python is as following:

def  function_name(parameters):

     statements

     return value


๐Ÿ” Explanation:


1."def" → keyword to define a function

2."function_name" → name of function

3."parameters" → inputs

4."statements" → code block

5."return" → output


Functions in Programming (Complete Beginner Guide),part 2




Comments