Types of
User-Defined Functions
1. Function with
No Argument and No Return
In this type of Function there are no arguments and also
does not return anything in back. An example of such function is as following:
v Example:
def myfunc():
print("My first
function")
myfunc()
·
✔
No input
·
✔
No return
·
✔
Only prints output
2. Function with
Argument but No Return
This type of functions have arguments but it does not return
any result in back. An examples of this type of functions are:
v
Example: Even Odd function
def evenodd(x):
if x % 2 == 0:
print("Even")
else:
print("Odd")
evenodd(10)
·
✔
Takes input
·
✔
Prints result
·
✔
No return value
v Example: Leap
Year Check
def leapyear(x):
if x % 4 == 0:
print("Leap year")
else:
print("Not a leap year")
3. Function with
Argument and Return
In these type of functions the function first takes the
input and also returns the output in back. An examples of such functions are as
following:
v Example: Sum Function
def sum(a, b):
return a + b
result = sum(2, 8)
print(result)
v Example: Multiplication Function
def mul(x, y):
return x * y
result = mul(4, 6)
print("mul
is:", result)
v Example: Subtraction Function
def sub(x, y):
return x - y
diff = sub(3, 2)
print(diff)
v
Example: Student result example with return
def marks(x):
if x >= 50:
return "Pass"
else:
return "Fail"
result = marks(70)
print(result)
Output:
Pass
🔁 Function Calling
After Defining the
function we use it in the program. To use a function, we call it by its name,
which we defined in the function definition. Calling a function means we are
activating a function. Calling a function means executing it. It is a necessary
to call a function before we use it in program.
v
Example:
myfunc()
·
Print vs Return
|
Feature |
Print() |
return |
|
Purpose |
Display Output |
Sends Value |
|
Usage |
Inside Function |
Inside Function |
|
Result |
Shows Immediately |
Can be stored in variable |
❓ Frequently Asked
Questions (FAQs)
Q1: What
is a function in simple words?
A function
is a block of code that performs a specific task in a program.
Q2: Why
are functions important?
They make
code reusable, clean, and easy to manage.
Q3: What
is the difference between print and return?
a. "print()"
shows output
b. "return" gives output back to the program
Q4: Can a
function work without return?
Yes, it
works without return but it will not send any value back.
Q5: What
are parameters?
Parameters are inputs given to a function so that it can
perform some tasks.
Conclusion
Functions
are a powerful feature in programming. They help us to write a clean,
efficient, and reusable code.
By
understanding the functions properly, we are able to take an important step
towards becoming a better programmer.


Comments
Post a Comment