Python Functions Mastery Your Complete Guide for Success
- Make a Function
- Calling a Function In python
- Function Arguments In Python
- The return Statement In Python
- The pass Statement In python
- Function Arguments in Python
- Keyword Argument In Python
- Function With Arbitrary Arguments In Python
A function is a block of code that performs a appointed task.
If we need to create a program to make a circle and color it. We can make two functions and solve this problem:
- function to make a circle
- function to color the shape
Partitive a complex problem into smaller lumps makes our program easy to understand and reuse.
¶Make a Function
Let’s make our first function.
def greet():
print('Hello World!')
Above are the different parts of the program:
Above, we have maked a simple function named greet()
which prints Hello World!
¶Calling a Function In python
In the upon example, we have expressed a function named greet()
.
def greet():
print('Hello World!')
Suppose we run the upon code, we won’t get an output.
It’s because making a function doesn’t mean we are executant the code inside it. That means the code is there for us to use if you want to.
For use this function, you need to call the function.
Function Call
greet()
¶Example: Function Call In Python
def greet():
print('Hello World!')
# call the function
greet()
print('Outside function')
Output :
Hello World!
Outside function
In the upon example, we have maked a function named greet()
. Below how the control of the program flows:
Below,,
- So that the function
greet()
is called, the program’s control disposal to the function definition. - All the code inside the function is evolved.
- The control of the program leaps to the next statement after the function call.
¶Function Arguments In Python
Arguments are inputs given to the function in Python.
def greet(name):
print("Hello", name)
# pass argument
greet("John")
OutPut:
Hello John
Above, we have passed 'John'
as an contention to the greet()
function.
We can pass several arguments in every call, making the function re-usable and progressive.
Now lets call the function with a several argument.
greet("David")
Output :
Hello David
¶Example: Function to Add Two Numbers In python
# function with two arguments
def add_numbers(num1, num2):
sum = num1 + num2
print("Sum: ", sum)
# function call with two values
add_numbers(5, 4)
Output :
Sum: 9
In the upon example, we have maked a function named add_numbers()
with arguments: num1
and num2
.
¶The return Statement In Python
We return a value from the function by using the return statement.
# function definition
def find_square(num):
result = num * num
return result
# function call
square = find_square(3)
print('Square:', square)
Output :
Square: 9
s
In the upon example, we have maked a function named find_square()
. The function receive a number and returns the square of the number.
¶The pass Statement In python
The pass
statement distributes as a placeholder for future code, prohibiting errors from empty code blocks.
It’s typically used where code is planned so that has yet to be written.
def future_function():
pass
# this will execute without any action or error
future_function()
Python Library Functions
Python take steps some built-in functions that can be right away used in our program.
We don’t requirement to make the function, we just need to call them.
Follow Python library functions are:
-
print()
- prints the string under the quotation marks -
sqrt()
- reports the group root of a number -
pow()
- reports the power of a number
This library functions are defined under the module. And to use them, we must comprise the module inside our program.
Now for example, sqrt()
is defined under the math module.
¶Example: Library Function In Python
import math
# sqrt computes the square root
square_root = math.sqrt(4)
print("Square Root of 4 is",square_root)
# pow() comptes the power
power = pow(2, 3)
print("2 to the power 3 is",power)
Output:
Square Root of 4 is 2.0
2 to the power 3 is 8
Below, we imported a math module to make the library functions sqrt()
and pow()
.
¶Function Arguments in Python
In computer python programming, an contention is a value that is accepted by a function.
¶Example 1: Function Arguments in Python
def add_numbers(a, b):
sum = a + b
print('Sum:', sum)
add_numbers(2, 3)
# Output: Sum: 5
In the upon example, the function add_numbers()
takes two parameters: a
and b
. Follow the line,
add_numbers(2, 3)
Below, add_numbers(2, 3)
give off that parameters a
and b
will get values 2 and 3 respectively.
¶Function Argument with Default Values In Python
In Python Program, we can bargain default values to function arguments.
We can use the =
operator to provide default values. Now for example,
def add_numbers( a = 7, b = 8):
sum = a + b
print('Sum:', sum)
# function call with two arguments
add_numbers(2, 3)
# function call with one argument
add_numbers(a = 2)
# function call with no arguments
add_numbers()
Output :
Sum: 5
Sum: 10
Sum: 15
In the upon example, notice the function definition-
def add_numbers(a = 7, b = 8):
Below, we have take measures default values 7 and 8 for parameters a and b respectively. Below how this program works
1. add_number(2, 3)
Both values are passed by the function call. Away, these values are used instead of the default values.
2. add_number(2)
Only one value is passed by the function call. So that, following to the positional argument 2 is assigned to argument a
, and the default value is used for parameter b
.
3. add_number()
No value is passed by the function call. Away, default value is used for both parameters a
and b
.
¶Keyword Argument In Python
def display_info(first_name, last_name):
print('First Name:', first_name)
print('Last Name:', last_name)
display_info(last_name = 'Cartman', first_name = 'Eric')
Output:
First Name: Eric
Last Name: Cartman
Follow the function call,
display_info(last_name = 'Cartman', first_name = 'Eric')
Below, we have assigned names to arguments by the function call.
Away, first_name
in the function call is assigned to first_name
in the function definition. likewise, last_name
in the function call is assigned to last_name
in the function definition.
¶Function With Arbitrary Arguments In Python
# program to find sum of multiple numbers
def find_sum(*numbers):
result = 0
for num in numbers:
result = result + num
print("Sum = ", result)
# function call with 3 arguments
find_sum(1, 2, 3)
# function call with 2 arguments
find_sum(4, 9)
Output:
Sum = 6
Sum = 13
In the upon example, we have maked the function find_sum()
that accepts unrestricted arguments. Follow the lines
find_sum(1, 2, 3)
find_sum(4, 9)
Variables in Python - Practical Examples of Python Variable
Python Datatypes Mastering the Essentials - Complete Guide
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development