Survey

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
```Programming and Data
Representation (Part-2)
Functions
Functions are "self contained" modules of code that
accomplish a specific task. Functions usually "take in"
data, process it, and "return" a result.
Once a function is written, it can be used over and over
and over again. Functions can be "called" from the inside
of other functions.
Functions
For example computing the square root of a number.
In general, we don't care how a function does what it
does, only that it "does it"!
Functions
●
When a function is "called" the program "leaves" the current section of code and
begins to execute the first line inside the function. Thus the function "flow of control"
is:
○ The program comes to a line of code containing a "function call".
○ The program enters the function (starts at the first line in the function code).
○ All instructions inside of the function are executed from top to bottom.
○ The program leaves the function and goes back to where it started from.
○ Any data computed and RETURNED by the function is used in place of the
function in the original line of code.
Functions
In pseudocode, a function definition is written as:
FUNCTION <functionIdentifier> RETURNS <dataType> // function
<statement(s)> // function body
RETURN <value>
ENDFUNCTION
Functions in Python
●
●
●
●
●
●
●
●
●
●
●
●
●
●
def getMax( set ):
max = set[0]
i = 1
while( i < 5 ):
if( max < set[i] ):
max = set[i]
i = i + 1
return max
set1 = [10, 20, 30, 40, 50]
set2 = [101, 201, 301, 401, 501]
max = getMax(set1)
print "Max in first set = ", max # Process first set of
numbers available in set1[]
max = getMax(set2)
print "Max in second set = ", max # Now process second
set of numbers available in set2[]
Built-in functions
Programming environments provide many built-in
functions. Some of them are always available to use;
some need to be imported from specialist module
libraries.
Built-in functions
Slicing in Python
In Python a subsequence of any sequence type (e.g.
lists and strings) can be created using ‘slicing’.
For example, to get a substring of length L from
position P in string S we write S[P : P + L].
Slicing in Python
ThisString[2:] CDEFG
ThisString[:2] AB
ThisString[-2:] FG
ThisString[:-2] ABCDE
Leap
Year
Calculation
Leap
Year
Calculation
Leap Year Calculation
Leap Year Calculation Flowchart
Code for Leap Year
year = int(input("enter the year: "))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print('%0.0f is a leap year' % year)
else:
print('%0.0f is not a leap year' % year)
else:
print('%0.0f is a leap year' % year)
else:
print('%0.0f is not a leap year' % year)
Converting a string to a number
Sometimes a whole number may be held as a string.
To use such a number in a calculation, we first need to
convert it to an integer. For example, these functions
return the integer value 5 from the string "5"
Consider the code:
grades = ["82", "84", "71", "64", "66"]
Output:
[82, 84, 71, 64, 66]
This code has successfully converted list of strings into a list of integers.
Random number generator
Random numbers are often required for simulations. Most
programming languages have various random number generators
available. As the random numbers are generated through a program,
they are referred to as ‘pseudo-random’ numbers.
Random number generator
Procedures
A procedure allows us to group a block of code under a name,
known as a procedure name. We can call the block of code from
anywhere in the program to execute the instructions it contains. We
can also pass values to the procedure to change how it works.
Procedures
print('1. Play game')
print('2. View high scores')
print('3. Quit')
Procedures
pi = 3.14
def updatePi(newPi):
global pi
pi = newPi
showAreaOfCircle(10)
updatePi(3.141)
showAreaOfCircle(10)
print('Area: ' + str(area))
Creating 1D arrays
Python
List1 = []
List1.append("Fred")
List1.append("Jack")
List1.append("Ali")
Creating 1D arrays
Java
String[] list1 = {"","",""};
int[] list2;
list2 = new int[5];
int[] list3;
list3 = new int[100];
String[] aList;
aList = new String[25];
Accessing 1D arrays
Python
NList[24] = 0
AList[3] = "D"
In Python, you can print the whole contents of a list using
print(List). In VB.NET and Java, you need to use a loop to
print one element of an array at a time.
```
Related documents