Skip to content

Commit 3a0eb81

Browse files
author
Saidev
committed
Added 'Functions'
1 parent 1046465 commit 3a0eb81

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

09_Functions.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
In Python, functions are defined using the `def` keyword, followed by the function name, parameters (optional), and a block of code. Functions are called by using their name followed by parentheses.
2+
3+
### 1. **Defining a Function**
4+
To define a function, you use the `def` keyword followed by the function name and any parameters inside parentheses. The code block that follows the function definition is indented.
5+
6+
```python
7+
def greet():
8+
print("Hello, welcome to Python!")
9+
```
10+
11+
In this example, `greet` is a function that doesn't take any parameters and simply prints a greeting message.
12+
13+
### 2. **Calling a Function**
14+
Once a function is defined, you can call it by writing its name followed by parentheses.
15+
16+
```python
17+
greet() # Calling the function
18+
```
19+
20+
### Output:
21+
```
22+
Hello, welcome to Python!
23+
```
24+
25+
### 3. **Function with Parameters**
26+
Functions can accept parameters, which allow you to pass values to the function when calling it.
27+
28+
```python
29+
def greet(name):
30+
print(f"Hello, {name}!")
31+
```
32+
33+
In this case, the `greet` function takes one parameter `name`.
34+
35+
### Calling the function with an argument:
36+
```python
37+
greet("Saidev") # Passing the argument "Saidev"
38+
```
39+
40+
### Output:
41+
```
42+
Hello, Saidev!
43+
```
44+
45+
### 4. **Function with Return Value**
46+
You can use the `return` keyword in a function to return a value to the caller. The `return` statement exits the function and sends a result back.
47+
48+
```python
49+
def add(a, b):
50+
return a + b
51+
```
52+
53+
### Calling the function and using the return value:
54+
```python
55+
result = add(3, 5)
56+
print(result)
57+
```
58+
59+
### Output:
60+
```
61+
8
62+
```
63+
64+
### 5. **Function with Default Parameters**
65+
You can define default values for function parameters. If a value is not passed when calling the function, the default value is used.
66+
67+
```python
68+
def greet(name="Guest"):
69+
print(f"Hello, {name}!")
70+
```
71+
72+
### Calling the function:
73+
```python
74+
greet("Alice") # Passing a value
75+
greet() # Using the default value
76+
```
77+
78+
### Output:
79+
```
80+
Hello, Alice!
81+
Hello, Guest!
82+
```
83+
84+
### 6. **Variable Scope in Functions**
85+
Variables defined inside a function are local to that function, meaning they can't be accessed outside of it.
86+
87+
```python
88+
x = 20
89+
90+
def example():
91+
x = 10 # Local variable
92+
print(x) # 10
93+
94+
print(x) # 20
95+
example()
96+
print(x) # 20
97+
```
98+
99+
### Summary:
100+
- **Defining a function**: Use `def` followed by the function name and parameters.
101+
- **Calling a function**: Use the function name followed by parentheses, passing arguments if required.
102+
- **Return values**: Use `return` to send values back from the function.
103+
- **Default parameters**: You can set default values for parameters to use when no argument is passed.
104+
- **Local variables**: Variables inside a function cannot be accessed outside the function.
105+
106+
Let me know if you need more examples or clarification!

0 commit comments

Comments
 (0)