0% found this document useful (0 votes)
7 views18 pages

ICT Lecture 26 28 (Functions)

Uploaded by

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

ICT Lecture 26 28 (Functions)

Uploaded by

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

CSC 101 – Applications of Information

and Communication Technology


Functions can be used to define
Introduction reusable code and organize and
simplify code.
 Suppose that you need to find the sum of integers from 1 to 10, 20 to 37, and
35 to 49.
 If you create a program to add these three sets of numbers, your code might
look like this:

2
Functions can be used to define
Introduction reusable code to organize and
simplify code.

 The preceding code can be simplified by using functions, as follows:

3
A function definition consists of
Defining a the function’s name, parameters,
Function and body.

 The syntax for defining a function is as follows:


def functionName(list of parameters)
# Function body

4
A function definition consists of
Defining a the function’s name, parameters,
Function and body.

 A function contains a header and body.


 The header begins with the def keyword, followed by the
function’s name and parameters, and ends with a colon.
 The variables in the function header are known as formal
parameters or simply parameters.
 A parameter is like a placeholder: When a function is invoked, you
pass a value to the parameter.
▪ This value is referred to as an actual parameter or argument.
▪ Parameters are optional; that is, a function may not have any parameters.
▪ For example, the random.random() function has no parameters.
 Some functions return a value, while other functions perform desired
operations without returning a value. If a function returns a value, it
is called a value-returning function.
5
Calling a function executes
Calling a Function the code in the function.

 In a function’s definition, you define what it is to do.


 To use a function, you have to call or invoke it.
 The program that calls the function is called a caller.
 There are two ways to call a function, depending on whether
or not it returns a value.

6
Calling a function executes
Calling a Function the code in the function.

 If the function returns a value, a call to that function is


usually treated as a value.
 For example,
larger = max(3, 4)
 calls max(3, 4) and assigns the result of the function to the variable larger.
 Another example of a call that is treated as a value is
print(max(3, 4))
 which prints the return value of the function call max(3, 4).
 If a function does not return a value, the call to the function
must be a statement.
 For example, the print function does not return a value. The
following call is a statement:
print("Programming is fun!")
7
8
9
Functions with/without Return Values

 A function does not have to return a value.


 Such a function is commonly known as a void function in programming terminology.

10
12
The scope of a variable is the
The Scope of part of the program where the
Variables variable can be referenced.

 A variable created inside a function is referred to as a local


variable.
 Local variables can only be accessed within a function.
 The scope of a local variable starts from its creation and continues to
the end of the function that contains the variable.
 In Python, you can also use global variables. They are created
outside all functions and are accessible to all functions in
their scope.

13
The scope of a variable is the
The Scope of part of the program where the
Variables variable can be referenced.

 A global variable is created in


line 1.
 It is accessed within the
function in line 4 and outside
the function in line 8.
 A local variable is created in
line 3.
 It is accessed within the
function in line 5.
 Attempting to access the
variable from outside of the
function causes an error in
line 9.
14
Python provides many useful
Common Python functions for common
Functions programming tasks.

 A function is a group of statements that performs a


specific task.
 Python, as well as other programming languages,
provides a library of functions.
 You have already used the functions eval, input,
print, and int.
 These are built-in functions and they are always
available in the Python interpreter.

15
Common Python Functions

16
Common Python Functions
''' Simple Python builtin functions
'''
num1, num2, num3 = -3, 16, 7.2
absNum = abs(num1) # Returns the absolute value
print ("Absolute value = ",absNum)

maxNum = max(num1, num2, num3) # Returns the maximum number


print("Maximum number = ", maxNum)

numPow = pow(num2, num3) # Same as num2 ** num3


print("numPow = ", numPow)

numR = round(num3) # Rounds to its nearest integer


numRd = round(3.1456, 3) # Rounds to 3 digits after the decimal
point
print("numR = ", numR , " numRd upto 3 = ", numRd)

17
18
Mathematical Functions

import math # import math module to use the math functions

#Test algebraic functions


print("log10(10, 10) =", math.log(10, 10))
print("sqrt(4.0) =", math.sqrt(4))

# Test trigonometric functions


print("tan(PI / 2) =", math.sin(math.pi/2))
print("degrees(1.57) =", math.degrees(1.57))
print("radians(90) =", math.radians(90))

19

You might also like