How to Define and Reuse Functions in Python

TL;DR
Python functions package a specific block of logic under a reusable name, reducing repeated code and making updates easier. Define one with the def keyword, place parameters inside parentheses, indent its body, and call it with the required arguments. Use pass when declaring a function whose body will be completed later.
Transcript
In today's video we'll learn about 'Functions' And "What are functions?" Sometimes we'd like to use our code's logic multiple times Let's suppose we want to print 1-15 15 times in our program And the code we wrote to perform this is of 10 lines We won't want to repeat these 10 lines in our program There are 2 reasons for this If we repeat these lin... Read More
Key Insights
- A function is a block of code that performs a specific task whenever it is called. It gives a reusable name to a piece of logic, allowing the programmer to invoke that behavior without rewriting the complete block each time.
- Repeated code increases both program length and maintenance work. If the same logic appears in 15 places, changing or correcting it may require updating all 15 copies, while a function centralizes that logic in one definition.
- Python functions are defined with the def keyword, followed by a function name and parentheses. The calculateGmean(a, b) example accepts two arguments, performs the tutorial's mean calculation, and prints the resulting value from its indented body.
- Function arguments provide values that the function can use during execution. The same calculateGmean definition is called with a and b, then with c and d, allowing one calculation block to process different pairs of variables.
- Indentation identifies which statements belong to a Python function. Statements in the function body require one indentation level, while statements inside an if or else within that function require another level to represent their nested structure.
- The isGreater(a, b) function packages comparison logic behind a short name. It prints that the first number is greater or equal when a is greater than b, otherwise it prints that the second number is greater.
- The pass statement acts as a temporary function body when implementation is postponed. Without a body, Python raises an indentation error, but pass lets the program continue while preserving the planned function name and accepted arguments.
- Functions help divide large programs into separate responsibilities. The tutorial describes an e-commerce program with logic for order processing, login checks, and pin-code serviceability, allowing different programmers to work on individual functions in parallel.
Install to Summarize YouTube Videos and Get Transcripts
Explore YouTube Video Summarizer or Get YouTube Transcript Extractor
Questions & Answers
Q: What is a function in Python?
A function in Python is a named block of code that performs a specific task whenever it is called. It can separate a piece of logic from the main program and make that logic reusable. Instead of copying a long block into many locations, the programmer defines it once and invokes its name wherever the same behavior is required.
Q: How do you define a function in Python?
A Python function is defined by writing the def keyword, the function name, parentheses containing any accepted arguments, and an indented body. The tutorial uses calculateGmean(a, b) and isGreater(a, b) as examples. The statements belonging to each function appear below its definition with indentation, which tells Python that they are part of that function.
Q: Why should repeated Python code be moved into a function?
Repeated code should be moved into a function because copying the same logic increases the number of lines and makes corrections harder. The tutorial considers logic used in 15 places. If that logic is copied, one change may need to be repeated 15 times. A function keeps the logic in one place and allows every call to reuse it.
Q: How do function arguments work in Python?
Function arguments supply the values that a function uses when it runs. In calculateGmean(a, b), a and b represent the two accepted inputs. The function can first be called with the variables a and b, then called again with c and d. Each call runs the same defined calculation using the values passed at that location.
Q: How does indentation affect a Python function?
Indentation marks the statements that belong to a function body. After a function definition, its internal statements must be indented. Nested logic needs additional indentation. In the isGreater example, the if and else structure belongs to the function, while each print statement is indented again to show that it belongs to the corresponding conditional branch.
Q: What does the isGreater function demonstrate?
The isGreater(a, b) example demonstrates how comparison logic can be placed in a reusable function. It checks whether a is greater than b. If so, it prints that the first number is greater or equal. Otherwise, it prints that the second number is greater. The function can then be called with multiple pairs of variables.
Q: When should you use pass in a Python function?
Use pass when you want to declare a function but plan to write its body later. Python expects an indented body after the definition, so leaving it empty causes an indentation error in the tutorial. Adding pass provides a temporary valid body, lets execution continue, and preserves the planned function name and its accepted arguments for later completion.
Q: How do functions help organize a large Python program?
Functions help organize a large program by separating its responsibilities into distinct blocks of logic. The tutorial describes an e-commerce website with functions for processing orders, checking whether a user is logged in, and checking pin-code serviceability. These separated tasks can also be assigned to different programmers, allowing work on several functions to proceed in parallel.
Summary & Key Takeaways
-
Functions separate frequently used logic from the main program so the same block does not need to be copied at every point of use. The tutorial demonstrates this by placing a calculation inside calculateGmean(a, b), then calling that function with two different pairs of variables while producing the same displayed results.
-
A function definition begins with def, followed by its name, accepted arguments, and an indented body. Another example places comparison logic inside isGreater(a, b). Calling isGreater with different variables executes the defined comparison, showing how a short function call can represent a larger block of instructions throughout a program.
-
Python expects an indented body after a function definition, even when the programmer intends to implement the logic later. Writing pass provides a temporary body and allows execution to continue without an indentation error. In larger programs, separate functions also let programmers divide responsibilities and work on different pieces in parallel.
Read in Other Languages (beta)
Share This Summary 📚
Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator
Explore More Summaries from CodeWithHarry 📚






Summarize YouTube Videos and Get Video Transcripts with 1-Click
Try YouTube Summary with ChatGPT & Claude or YouTube Transcript Generator