0% found this document useful (0 votes)
3 views3 pages

Module 8 Functions

Uploaded by

scemer213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Module 8 Functions

Uploaded by

scemer213
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Bootcamp - Module 9

PYTHON FUNCTIONS

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

To call a function, use the function name followed by parenthesis

Example

def my_function(): #FUNCTION DEFINITION


print("Hello from a function")

my_function() #FUNCTION CALL

ARGUMENTS

Information can be passed into functions as arguments.


Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (f name).
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 ("Thomas")
my_function ("Alva")
my_function ("Edison")

# A simple Python function to check


# whether x is even or odd

def evenOdd( x ):
if (x % 2 == 0):
print "even"
else:
print "odd"

evenOdd(2)
evenOdd(3)
Python Bootcamp - Module 9
PYTHON FUNCTIONS

def swap(x, y):


temp = x;
x = y;
y = temp;

x=2
y=3
swap(x, y)
print(x)
print(y)

NUMBER OF ARGUMENTS

By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments,
you have to call the function with 2 arguments, not more, and not less.

If you try to call the function with 1 or 3 arguments, you will get an error:

Example :
This function expects 2 arguments, but gets only 1:

def my_function(fname, lname):


print(fname + " " + lname)

my_function("Emil")

RETURN VALUES
To let a function return a value, use the return statement:

def my_function(x):
return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9))
Python Bootcamp - Module 9
PYTHON FUNCTIONS

EXERCISES

1 Create a function to find out whether a number is prime or not


2. Create a function which takes a list as an argument and append into it the value 7.
2. Write a Python function to find the Max of three numbers.
4. Write a function to print the even numbers from a given list.
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
5. Write a Python function that checks whether a passed string is palindrome or not.

You might also like