Notes
functions in python
Content of the notes (2)
Page 1
"functions in python"
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
A function can return data as a result.
Creating a Function:
-------------------------------
In Python a function is defined using the def keyword:
Example:
def my_function():
print("Hello from a function")
Calling a Function:
----------------------------
example:
def my_function():
print("Hello from a function")
my_function()
output:
Hello from a function
Parameters:
-----------------
Information can be passed to functions as parameter.
Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.
The following example has a function with one parameter (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
output:
-----------
Emil Refsnes
Tobias Refsnes
Linus Refsnes
Page 2
Information
- Description
- Note on functions in python, created by Sandeep Sunny on 05/08/2019.
- Created by
- Sandeep Sunny · Aug 2019
- Updated
- Aug 5, 2019
- Views
- 4
- Tags
- Rating