SlideShare a Scribd company logo
1 of 19
Lab 10
CS106 - Fundamentals of Computer Science
Presented by TA. Nada Kamel
Agenda
 How to sort an array ?
 Functions (passing by value)
 Example
 Hands-on problem
Presented by TA. Nada Kamel
Bubble Sort
Presented by TA. Nada Kamel
Sorting an array using bubble sort
Presented by TA. Nada Kamel
Bubble sort
do {
flag = false;
for (int j = 0; j < n; j++)
{
if (A[j] > A[j+1])
{
int temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
flag = true;
}
}
} while(flag);
Presented by TA. Nada Kamel
Functions
Presented by TA. Nada Kamel
User defined functions
 C++ allows programmer to define their own function.
 A user-defined function groups code to perform a specific task and that group of
code is given a name(identifier).
 When the function gets called from any part of program, it all executes the codes
defined in the body of function.
Presented by TA. Nada Kamel
Function Definition
return_type function_name( parameters list )
{
// body of the function
}
Presented by TA. Nada Kamel
Example 1:
void display_welcome_message(string name)
{
// body of the function
cout << “Hello, ” << name << endl;
}
Example 2:
int maximum(int num1, int num2)
{
int result; // local variable declaration
if(num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function prototype (declaration)
Function Prototype MUST be written before the main() function.
return_type function_name( parameters list );
Example:
int max(int, int); (OR) int max(int num1, int num2);
Note: It is not necessary to define prototype if user-defined function exists
before main() function.
Presented by TA. Nada Kamel
Function Call
 To call a function, you simply need to pass the required parameters
along with function name.
 If function returns a value, then you can store the returned value in a
variable.
Presented by TA. Nada Kamel
Example
Presented by TA. Nada Kamel
Example
Write a C++ program to add two integers.
Make a function add() to add integers and display sum in main() function.
Presented by TA. Nada Kamel
#include <iostream>
using namespace std;
// Function prototype (declaration)
int add(int, int);
int main() {
int num1, num2, sum;
cout << "Enter two numbers to add: ";
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << "Sum = " << sum << endl;
return 0;
}
// Function definition
int add(int a, int b) {
int add;
add = a + b;
// Return statement
return add;
}
Hands-on Problem
Presented by TA. Nada Kamel
Problem
Write a C++ function that takes a positive integer number and
returns its factorial.
Mathematically, factorial of an integer number n is referred to as
n! and defined as follows:
n! = n x (n-1) x (n-2) x ...... x 3 x 2 x1 for n > 1, and 0! = 1
Presented by TA. Nada Kamel
Presented by TA. Nada Kamel
Any Questions?
Presented by TA. Nada Kamel

More Related Content

What's hot

Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
ecko_disasterz
 

What's hot (18)

Tail Recursion in data structure
Tail Recursion in data structureTail Recursion in data structure
Tail Recursion in data structure
 
CS106 Lab 5 - Switch case
CS106 Lab 5 - Switch caseCS106 Lab 5 - Switch case
CS106 Lab 5 - Switch case
 
20BCE1734.pdf
20BCE1734.pdf20BCE1734.pdf
20BCE1734.pdf
 
Effective java item 80 and 81
Effective java   item 80 and 81Effective java   item 80 and 81
Effective java item 80 and 81
 
Functions in c++,
Functions in c++,Functions in c++,
Functions in c++,
 
applet.docx
applet.docxapplet.docx
applet.docx
 
Code Tuning
Code TuningCode Tuning
Code Tuning
 
Evaluation of prefix expression with example
Evaluation of prefix expression with exampleEvaluation of prefix expression with example
Evaluation of prefix expression with example
 
Lecture4
Lecture4Lecture4
Lecture4
 
P3
P3P3
P3
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Functions & Procedures [7]
Functions & Procedures [7]Functions & Procedures [7]
Functions & Procedures [7]
 
Asymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using CAsymptotic Analysis in Data Structure using C
Asymptotic Analysis in Data Structure using C
 
Filter Designing
Filter DesigningFilter Designing
Filter Designing
 
Exp 3-2 d422 (1)
Exp 3-2  d422 (1)Exp 3-2  d422 (1)
Exp 3-2 d422 (1)
 
Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions Conversion of Infix To Postfix Expressions
Conversion of Infix To Postfix Expressions
 
Phase Responce of Pole zero
Phase Responce of Pole zeroPhase Responce of Pole zero
Phase Responce of Pole zero
 
Minimum phase, All pass and Magnitude Squared Function
Minimum phase, All pass and Magnitude Squared FunctionMinimum phase, All pass and Magnitude Squared Function
Minimum phase, All pass and Magnitude Squared Function
 

Similar to CS106 Lab 10 - Functions (passing by value)

Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
 

Similar to CS106 Lab 10 - Functions (passing by value) (20)

Array Cont
Array ContArray Cont
Array Cont
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Write a Java application that asks for an integer and returns its fac.pdf
Write a Java application that asks for an integer and returns its fac.pdfWrite a Java application that asks for an integer and returns its fac.pdf
Write a Java application that asks for an integer and returns its fac.pdf
 
OOP program questions with answers
OOP program questions with answersOOP program questions with answers
OOP program questions with answers
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Function in c
Function in cFunction in c
Function in c
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Function in c
Function in cFunction in c
Function in c
 
Functions.pptx
Functions.pptxFunctions.pptx
Functions.pptx
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 

Recently uploaded (20)

How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 

CS106 Lab 10 - Functions (passing by value)

  • 1. Lab 10 CS106 - Fundamentals of Computer Science Presented by TA. Nada Kamel
  • 2. Agenda  How to sort an array ?  Functions (passing by value)  Example  Hands-on problem Presented by TA. Nada Kamel
  • 3. Bubble Sort Presented by TA. Nada Kamel
  • 4. Sorting an array using bubble sort Presented by TA. Nada Kamel
  • 5. Bubble sort do { flag = false; for (int j = 0; j < n; j++) { if (A[j] > A[j+1]) { int temp = A[j]; A[j] = A[j+1]; A[j+1] = temp; flag = true; } } } while(flag);
  • 6. Presented by TA. Nada Kamel
  • 8. User defined functions  C++ allows programmer to define their own function.  A user-defined function groups code to perform a specific task and that group of code is given a name(identifier).  When the function gets called from any part of program, it all executes the codes defined in the body of function. Presented by TA. Nada Kamel
  • 9. Function Definition return_type function_name( parameters list ) { // body of the function } Presented by TA. Nada Kamel
  • 10. Example 1: void display_welcome_message(string name) { // body of the function cout << “Hello, ” << name << endl; } Example 2: int maximum(int num1, int num2) { int result; // local variable declaration if(num1 > num2) result = num1; else result = num2; return result; }
  • 11. Function prototype (declaration) Function Prototype MUST be written before the main() function. return_type function_name( parameters list ); Example: int max(int, int); (OR) int max(int num1, int num2); Note: It is not necessary to define prototype if user-defined function exists before main() function. Presented by TA. Nada Kamel
  • 12. Function Call  To call a function, you simply need to pass the required parameters along with function name.  If function returns a value, then you can store the returned value in a variable. Presented by TA. Nada Kamel
  • 14. Example Write a C++ program to add two integers. Make a function add() to add integers and display sum in main() function. Presented by TA. Nada Kamel
  • 15. #include <iostream> using namespace std; // Function prototype (declaration) int add(int, int); int main() { int num1, num2, sum; cout << "Enter two numbers to add: "; cin >> num1 >> num2; // Function call sum = add(num1, num2); cout << "Sum = " << sum << endl; return 0; } // Function definition int add(int a, int b) { int add; add = a + b; // Return statement return add; }
  • 17. Problem Write a C++ function that takes a positive integer number and returns its factorial. Mathematically, factorial of an integer number n is referred to as n! and defined as follows: n! = n x (n-1) x (n-2) x ...... x 3 x 2 x1 for n > 1, and 0! = 1 Presented by TA. Nada Kamel
  • 18. Presented by TA. Nada Kamel
  • 19. Any Questions? Presented by TA. Nada Kamel