Skip to main content

User Defined Function | Python Programming By Akash

1.   Functions:

Definition: It is a named block of code that performs a specific task. Instead of writing the same code again and again, we define it once and can use (call) it multiple times anywhere in the program. It runs only when we explicitly 'call' it.

Types of Functions:

a.   Buit-in Functions: These are pre-defined functions that come with Python. You don't need to write the code for them; you just use them.

b.   User-Defined Functions: These are functions that we create ourselves using the def keyword to solve our specific problems.

c.   Anonymous Functions: These are short, one-line functions that do not have a name. They are used for quick, short tasks.

2.   User-Defined Functions:

Definition: These are functions that you create to perform a specific task relevant to your software. While Python gives you general tools (like print), User-Defined Functions are the custom tools you build for your specific job (like calculate_beam_load).

The 4 Pillars of a Function:

a.   def Keyword: Tells Python, "I am about to create a new function."

b.   Function Name: A unique name to identify it (e.g., add_numbers). Follows the same naming rules as variables (snake_case).

c.   Parameters (Inputs): Variables listed inside () that receive data. They act as placeholders.

d.   Function Body (Code Block): The indented code that does the actual work.

The Syntax: def function_name(parameter1, parameter2):

For Example,

3.   Parameters (The Placeholders):

Definition: These are the variables listed inside the parentheses when you define the function.

4.   Arguments (The Real Data):

Definition: These are the actual values you send to the function when you call it.

For Example,

5.   The Return Statement (return):

Definition: The return keyword is used to send a value out of the function and back to the main program. It marks the end of the function's execution. Unlike print() (which just displays text on the screen), return gives the actual data back to the code so you can store it in a variable and use it for further calculations.

For Example,




Comments