SlideShare a Scribd company logo
1 of 18
CTE 313
COMPUTER PROGRAMMING
LECTURE 3
LECTURE RECAP
Topics covered so far:
Lecture 1:
◦ Course Information & Introduction to Python Programming.
◦ Input, processing and Output
Lecture 2:
◦ Decision Making and Loop
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 2
LECTURE 3
Topic to cover:
 Functions
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 3
LECTURE OBJECTIVES
 To understand why programmers divide programs up into sets of
cooperating functions.
To be able to define new functions in Python.
To understand the details of function calls and parameter passing in
Python.
To write programs that use functions to reduce code duplication and
increase program modularity.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 4
Functions
The programs that we have written so far comprise a single function,
usually called main.
A function is a group of statements that exist within a program for the
purpose of performing a specific task.
Instead of writing a large program as one long sequence of statements, it
can be written as several small functions, each one performing a specific
part of the task.
These small functions can then be executed in the desired order to
perform the overall task.
A program that has been written with each task in its own function is
called a modularized program.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 5
Benefits of Modularizing a Program with Functions
Simpler Code
Code Reuse
Better Testing
Faster Development
Easier Facilitation of Teamwork
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 6
Defining and Calling a Function
The code for creating a function is known as a function definition.
To execute the function, you write a statement that calls it.
The general format of a function definition in Python:
o def function_name():
o statement
o statement
o etc.
 The first line is known as the function header. It marks the beginning of the function
definition.
 The function header begins with the key word def, followed by the name of the function,
followed by a set of parentheses, followed by a colon.
 Beginning at the next line is a set of statements known as a block (a set of statements that
belong together as a group).
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 7
A call to a function initiates a four step process:
 The calling program is suspended.
 The values of actual parameters are assigned to the formal parameters.
 The body of the function is executed.
 Control returns immediately following the function call in the calling program.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 8
Exercise 1:
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 9
Exercise 2: More interesting…
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 10
Local Variables
A local variable is created inside a function and cannot be
accessed by statements that are outside the function.
Different functions can have local variables with the same
names because the functions cannot see each other’s local
variables.
Anytime you assign a value to a variable inside a function, you
create a local variable.
The term local is meant to indicate that the variable can be
used only locally, within the function in which it is created.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 11
Exercise 3: Using Local Variable
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 12
Passing Arguments to Functions
An argument is any piece of data that is passed into a function
when the function is called.
A parameter is a variable that receives an argument that is
passed into a function.
Sometimes it is useful not only to call a function, but also to
send one or more pieces of data into the function.
Pieces of data that are sent into a function are known as
arguments.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 13
Exercise 4: simple argument
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 14
Exercise 5: Using argument for conversion
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 15
Exercise 6: Passing Multiple Arguments
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 16
QUESTIONS
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 17
Assignment
1. There are three seating categories at a stadium. For a football game, Class A seats cost
N2000, Class B seats cost N1000, and Class C seats cost N500. Write a program that asks
how many tickets for each class of seats were sold, and then displays the amount of
income generated from ticket sales.
2. Write a program that calculates and displays a person’s body mass index (BMI). The BMI
is often used to determine whether a person is overweight or underweight for his or her
height. A person’s BMI is calculated with the following formula:
BMI = weight x 703 / height
where weight is measured in kg and height is measured in meters.
N.B: Use Function for each task.
A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 18

More Related Content

Similar to CTE 313 - Lecture 3.pptx

Functions in c
Functions in cFunctions in c
Functions in creshmy12
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ pptMalarMohana
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ pptsujathavvv
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Frequently asked tcs technical interview questions and answers
Frequently asked tcs technical interview questions and answersFrequently asked tcs technical interview questions and answers
Frequently asked tcs technical interview questions and answersnishajj
 
C basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaC basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaManoj Kumar kothagulla
 
Bca1020 programming in c
Bca1020  programming in cBca1020  programming in c
Bca1020 programming in csmumbahelp
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1Syed Farjad Zia Zaidi
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignmentAhmad Kamal
 
Api specification based function search engine using natural language query-S...
Api specification based function search engine using natural language query-S...Api specification based function search engine using natural language query-S...
Api specification based function search engine using natural language query-S...Sanif Sanif
 
Oop in c++ lecture 1
Oop in c++  lecture 1Oop in c++  lecture 1
Oop in c++ lecture 1zk75977
 

Similar to CTE 313 - Lecture 3.pptx (20)

Functions in c
Functions in cFunctions in c
Functions in c
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ ppt
 
Programming in c++ ppt
Programming in c++ pptProgramming in c++ ppt
Programming in c++ ppt
 
Book management system
Book management systemBook management system
Book management system
 
My c++
My c++My c++
My c++
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
cpFunctions-Files.pptx
cpFunctions-Files.pptxcpFunctions-Files.pptx
cpFunctions-Files.pptx
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
EEE 3rd year oops cat 3 ans
EEE 3rd year  oops cat 3  ansEEE 3rd year  oops cat 3  ans
EEE 3rd year oops cat 3 ans
 
Frequently asked tcs technical interview questions and answers
Frequently asked tcs technical interview questions and answersFrequently asked tcs technical interview questions and answers
Frequently asked tcs technical interview questions and answers
 
C basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kellaC basic questions&ansrs by shiva kumar kella
C basic questions&ansrs by shiva kumar kella
 
Lecture 11 - Functions
Lecture 11 - FunctionsLecture 11 - Functions
Lecture 11 - Functions
 
PYTHON PPT.pptx
PYTHON PPT.pptxPYTHON PPT.pptx
PYTHON PPT.pptx
 
Bca1020 programming in c
Bca1020  programming in cBca1020  programming in c
Bca1020 programming in c
 
Introduction To Programming with Python-1
Introduction To Programming with Python-1Introduction To Programming with Python-1
Introduction To Programming with Python-1
 
Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Api specification based function search engine using natural language query-S...
Api specification based function search engine using natural language query-S...Api specification based function search engine using natural language query-S...
Api specification based function search engine using natural language query-S...
 
Oop in c++ lecture 1
Oop in c++  lecture 1Oop in c++  lecture 1
Oop in c++ lecture 1
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 

Recently uploaded

History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationEmaan Sharma
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...IJECEIAES
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligencemahaffeycheryld
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfKira Dess
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxMustafa Ahmed
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailingAshishSingh1301
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentationsj9399037128
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksIJECEIAES
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...archanaece3
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUankushspencer015
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsMathias Magdowski
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxKarpagam Institute of Teechnology
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfragupathi90
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..MaherOthman7
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxMustafa Ahmed
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological universityMohd Saifudeen
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...Amil baba
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisDr.Costas Sachpazis
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdfKamal Acharya
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxkalpana413121
 

Recently uploaded (20)

History of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & ModernizationHistory of Indian Railways - the story of Growth & Modernization
History of Indian Railways - the story of Growth & Modernization
 
Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...Developing a smart system for infant incubators using the internet of things ...
Developing a smart system for infant incubators using the internet of things ...
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
Artificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdfArtificial intelligence presentation2-171219131633.pdf
Artificial intelligence presentation2-171219131633.pdf
 
Dynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptxDynamo Scripts for Task IDs and Space Naming.pptx
Dynamo Scripts for Task IDs and Space Naming.pptx
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
Seizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networksSeizure stage detection of epileptic seizure using convolutional neural networks
Seizure stage detection of epileptic seizure using convolutional neural networks
 
5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...5G and 6G refer to generations of mobile network technology, each representin...
5G and 6G refer to generations of mobile network technology, each representin...
 
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTUUNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
UNIT-2 image enhancement.pdf Image Processing Unit 2 AKTU
 
Filters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility ApplicationsFilters for Electromagnetic Compatibility Applications
Filters for Electromagnetic Compatibility Applications
 
analog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptxanalog-vs-digital-communication (concept of analog and digital).pptx
analog-vs-digital-communication (concept of analog and digital).pptx
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
NO1 Best Powerful Vashikaran Specialist Baba Vashikaran Specialist For Love V...
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Insurance management system project report.pdf
Insurance management system project report.pdfInsurance management system project report.pdf
Insurance management system project report.pdf
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 

CTE 313 - Lecture 3.pptx

  • 2. LECTURE RECAP Topics covered so far: Lecture 1: ◦ Course Information & Introduction to Python Programming. ◦ Input, processing and Output Lecture 2: ◦ Decision Making and Loop A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 2
  • 3. LECTURE 3 Topic to cover:  Functions A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 3
  • 4. LECTURE OBJECTIVES  To understand why programmers divide programs up into sets of cooperating functions. To be able to define new functions in Python. To understand the details of function calls and parameter passing in Python. To write programs that use functions to reduce code duplication and increase program modularity. A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 4
  • 5. Functions The programs that we have written so far comprise a single function, usually called main. A function is a group of statements that exist within a program for the purpose of performing a specific task. Instead of writing a large program as one long sequence of statements, it can be written as several small functions, each one performing a specific part of the task. These small functions can then be executed in the desired order to perform the overall task. A program that has been written with each task in its own function is called a modularized program. A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 5
  • 6. Benefits of Modularizing a Program with Functions Simpler Code Code Reuse Better Testing Faster Development Easier Facilitation of Teamwork A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 6
  • 7. Defining and Calling a Function The code for creating a function is known as a function definition. To execute the function, you write a statement that calls it. The general format of a function definition in Python: o def function_name(): o statement o statement o etc.  The first line is known as the function header. It marks the beginning of the function definition.  The function header begins with the key word def, followed by the name of the function, followed by a set of parentheses, followed by a colon.  Beginning at the next line is a set of statements known as a block (a set of statements that belong together as a group). A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 7
  • 8. A call to a function initiates a four step process:  The calling program is suspended.  The values of actual parameters are assigned to the formal parameters.  The body of the function is executed.  Control returns immediately following the function call in the calling program. A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 8
  • 9. Exercise 1: A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 9
  • 10. Exercise 2: More interesting… A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 10
  • 11. Local Variables A local variable is created inside a function and cannot be accessed by statements that are outside the function. Different functions can have local variables with the same names because the functions cannot see each other’s local variables. Anytime you assign a value to a variable inside a function, you create a local variable. The term local is meant to indicate that the variable can be used only locally, within the function in which it is created. A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 11
  • 12. Exercise 3: Using Local Variable A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 12
  • 13. Passing Arguments to Functions An argument is any piece of data that is passed into a function when the function is called. A parameter is a variable that receives an argument that is passed into a function. Sometimes it is useful not only to call a function, but also to send one or more pieces of data into the function. Pieces of data that are sent into a function are known as arguments. A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 13
  • 14. Exercise 4: simple argument A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 14
  • 15. Exercise 5: Using argument for conversion A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 15
  • 16. Exercise 6: Passing Multiple Arguments A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 16
  • 17. QUESTIONS A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 17
  • 18. Assignment 1. There are three seating categories at a stadium. For a football game, Class A seats cost N2000, Class B seats cost N1000, and Class C seats cost N500. Write a program that asks how many tickets for each class of seats were sold, and then displays the amount of income generated from ticket sales. 2. Write a program that calculates and displays a person’s body mass index (BMI). The BMI is often used to determine whether a person is overweight or underweight for his or her height. A person’s BMI is calculated with the following formula: BMI = weight x 703 / height where weight is measured in kg and height is measured in meters. N.B: Use Function for each task. A.O. AGBEYANGI - CHRISLAND UNIVERSITY (CSC) 18