-
Notifications
You must be signed in to change notification settings - Fork 3
Functions & in built functions
We have been using functions up till now even in basic programs but didn't realize it. Now, we will dig into this topic. we used print() ,input() function to print message, value to console screen and take user input in console screen. we re using these functions but we have not created it. Compiler uses the library which are by default imported to our code to use these functions.
Any peace of code written for a specific and single purpose which can be further used can be described using name and the code is enclosed inside this function name. Every-time one wants to reuse the functionality implemented, can use this function by calling it and the piece of code will execute.
In a way, this is used for code reuse and we don't have to write duplicate code every-time we need same functionality. In big systems, we have multiple instances where we need to run same piece of code for different scenarios or different purpose. So we enclose that logic in functions.
Function can have any combination of these characteristics i.e. can have 1,2,3 all of them or 1,2 or any combination:
- Function can take input .Function can need some parameters to do some processing.
- function can give output .These functions return values to the caller so that caller can use this calculated value from the function further.
- function can process something inside and don't give any output. These functions just do action.
Every function has an address in memory. Want to check?
print(function_name)
will give address.
Inputs to function while we are declaring are the parameters of the function.
Passing values to function parameters are called arguments. These arguments are passed while calling the function.
Some in-built functions are :
- dir() -This function will return all the properties and methods, even built-in properties which are default for all object. Basically will get information about the object's functions.
- help - Get documentation of modules, functions, classes, keywords .
- range() -function that allows user to generate a series of numbers within a given range.
- Print() - print function to print message.
a variable can also be assigned a function. all the logic written is function is copied to the variable and when you call this variable
same logic can be called.
This variable will have separate copy of the logic and will not refer to the function address as reference. This means change in function later will not be reflected to the logic this variable has.
Ex.
def func():
some logic here
variable = func
#variable has the logic of function now. All the code in the function is copied to the variable.
One of the instances where function can passed to function can be
-> In case you want user to give their custom logic for a part of code in functions you created, so that their code runs as part of your function defined, you can have this implemented.
def func1():
print("custom logic added to existing function succesfully")
def func2(logicToRun):
statement1
logicToRun()
statement2
func2(func1)
Keep Learning. Never Settle! 😊
- Home
- 1.Introduction To Python
- 2. DataTypes
- 3. Strings and Slicing of collection
- 4.If else & Loops
- 5. Break, continue, pass
- 6. Functions
- 7. Modules
- 8. Clarifying basics
- 9. File Handling
- 10. Exception Handling
- 11. Dig into Collections
- 12. Iterators, Generators and list comprehension
- 13. lambdas , map, filter and other concepts
- 14. args, kwargs an default parameters
- 15. Functional Programming
- 16. OOPs