Skip to content

Commit c4c97c8

Browse files
committed
added the differences b/w return and print keywords in the function
1 parent b4c4226 commit c4c97c8

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

Day-04/demo.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
def add(a, b):
22
c = a + b
33
return c
4-
addition = add(2, 4)
4+
# addition = add(2, 4)
55
def sub(a, b):
66
c = a - b
77
return c
@@ -12,7 +12,7 @@ def div(a, b):
1212
c = a % b
1313
return c
1414
print(add(2, 4))
15-
print(addition)
16-
print(sub(2, 4))
17-
print(mul(2, 4))
18-
print(div(2, 4))
15+
# print(addition)
16+
# print(sub(2, 4))
17+
# print(mul(2, 4))
18+
# print(div(2, 4))

Day-05/sample.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def add_numbers(a, b):
2+
result = a + b # Add the two numbers
3+
print(result) # Print the result directly
4+
# return result
5+
6+
# Call the function
7+
sum_value = add_numbers(5, 3)
8+
# print(sum_value) # Output will be None

Day-05/sample1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Function 1: Add two numbers and print the result
2+
def add_numbers(a, b):
3+
result = a + b
4+
print(result) # Print the result directly
5+
6+
# Function 2: Multiply two numbers and print the result
7+
def multiply_numbers(a, b):
8+
result = a * b
9+
print(result) # Print the result directly
10+
11+
# Call the functions
12+
add_numbers(5, 3) # This will print 8 directly
13+
multiply_numbers(8, 2) # This will print 16 directly
14+
15+
# Trying to store the result will not work as expected
16+
sum_value = add_numbers(5, 3) # This will print 8, but sum_value will be None
17+
product_value = multiply_numbers(8, 2) # This will print 16, but product_value will be None
18+
19+
# Output the variables (which will be None)
20+
print("Sum:", sum_value) # Output will be None
21+
print("Product:", product_value) # Output will be None
22+
23+
24+
# How print works in this example:
25+
# add_numbers(5, 3) prints 8 directly, but it does not return anything. Therefore, sum_value will be None.
26+
# multiply_numbers(8, 2) prints 16 directly, but similarly, product_value will be None.
27+
# When we try to print sum_value and product_value, they will both be None because the functions don’t return values.
28+
# Key Differences:
29+
# With return: The function returns the result, which can be stored and reused. You have more flexibility to use the values.
30+
# With print: The function directly prints the result, but you cannot store or reuse the value in the calling code. Any attempt to store the result will lead to None.

Day-05/sample2.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Function 1: Add two numbers and return the result
2+
def add_numbers(a, b):
3+
result = a + b
4+
return result # Return the result to the caller
5+
6+
# Function 2: Multiply two numbers and return the result
7+
def multiply_numbers(a, b):
8+
result = a * b
9+
return result # Return the result to the caller
10+
11+
# Call the functions and use their return values
12+
sum_value = add_numbers(5, 3)
13+
product_value = multiply_numbers(sum_value, 2)
14+
15+
# Output the results
16+
print("Sum:", sum_value) # Output will be 8
17+
print("Product:", product_value) # Output will be 16
18+
19+
20+
# How return works in this example:
21+
# add_numbers(5, 3) returns the sum 8, which is stored in sum_value.
22+
# multiply_numbers(sum_value, 2) uses sum_value and multiplies it by 2, returning 16, which is stored in product_value.
23+
# Both results are printed using print after the function calls.

0 commit comments

Comments
 (0)