SlideShare a Scribd company logo
FUNCTIONS
LAB 2
WHAT ARE FUNCTIONS?
A function is a block of code that performs a specific task.
Functions are used to call a formula in the main program
It is not included in the main body
Can be called number of times
TYPES
◦Standard functions
◦User-defined functions
STANDARD FUNCTIONS
These standard functions are groups in different libraries
which can be included in the C++ program, e.g math.h,
iostream.h
USERDEFINEDFUNCTIONS
Although C++ is shipped with a lot of standard functions,
these functions are not enough for all users, therefore, C++
provides its users with a way to define their own functions
(or user-defined function)
DEFINING FUNCTIONS
A C++ function consists of two parts
The function header, and
The function body
The function header has the following syntax
<return type> <name> (<parameter list>)
The function body is simply a C++ code enclosed between { }
EXAMPLE
int sum(int a, int b){
int c = a+b;
return c;
}
EXAMPLE- (CALLING A FUNCTION)
int main(){
int num1, num2;
cout<<"Enter first number"<<endl;
cin>>num1;
cout<<"Enter second number"<<endl;
cin>>num2;
cout<<"The sum is "<<sum(num1, num2);
return 0;
}
FUNCTION PROTOTYPE
A function prototype is simply the declaration of a function that
specifies function's name, parameters and return type. It doesn't
contain function body.
A function prototype gives information to the compiler that the
function may later be used in the program.
SYNTAX OF FUNCTION PROTOTYPE
returnType functionName(type1 argument1, type2 argument2, ...);
Int sum (int a, int b)
Declaration: int sum(int, int);
PASSING ARGUMENTS TO A FUNCTION
In programming, argument refers to the variable passed to the function. In the above example,
two variables n1 and n2 are passed during the function call.
The parameters a and b accepts the passed arguments in the function definition. These arguments
are called formal parameters of the function.
RETURNSTATEMENT
The return statement terminates the execution of a function and returns a value to the calling
function.
The void function is used when no return value is needed
EXAMPLE:FINDWHETHERANUMBERISEVENORODD
void eo(int x)
{
if (x%2 == 0)
cout << "Evenn";
else
cout << "Oddn";
}
int main()
{
eo(4);
eo(5);
return 0;
}
PASSING ARRAYS AS
ARGUMENTS TO FUNCTIONS
Void max( float [], int []);
Void temp( float[][], int[][]);
DECLARATIONOFAFUNCTIONWITHARRAYARGUMENTS
PROGRAM TO FIND A
NUMBER AND ITS
POSITION IN AN ARRAY
USING FUNCTIONS
FUNCTION CALL BY
REFERENCE
When a function is called, the values that are passed in the call are called arguments or actual parameters
( The actual parameters are x and y in this case)
Formal parameters are local variables which are assigned values from the arguments when the function is
called. ( The formal parameters are a and b in this case which are assigned the values of x and y, to perform
the operation)
int add( int a, int b);
int main()
{ int x=3; y=4;
add(x,y)}
ACTUALANDFORMALPARAMETERS
Call by value
Call by reference
TYPESOFFUNCTIONCALLS
When the function is called in the main function, the values given to actual parameters are
copied to formal parameters of the function and the operation is performed
This means that the values assigned to actual parameters in the main function remain unchanged
no matter whatever the operations are performed in the function
POINTSTOREMEMEBER (CALLBYVALUE)
In call of call by reference, the address is copied to the function using pointers
This means that the value stored at the address in the main function can be changed using
functions
POINTSTOREMEMEBER (CALLBYREFERENCE)
int add(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
(code in the notes below)
CALLBYREFERENCE(SWAPEXAMPLE)
CLASSTASK (MAHDEPT)
Make a mathematical calculator using functions ( addition, subtraction,
multiplication, division)
1. Get two values a and b, and change the values of a to a=a+b; and b=a-b; using call by
reference
2. Define two functions to print the maximum and the minimum number respectively among
three numbers entered by user.
3. Write a program to print the circumference and area of a circle of radius entered by user by
defining your own function.
HOMETASKS(MATHDEPT)

More Related Content

DOC
Functions
PPTX
Module 3-Functions
PPTX
UNIT3.pptx
PPT
POLITEKNIK MALAYSIA
PDF
Programming in C Functions PPT Presentation.pdf
PDF
programlama fonksiyonlar c++ hjhjghjv jg
PPT
Unit iv functions
PPT
Fp201 unit5 1
Functions
Module 3-Functions
UNIT3.pptx
POLITEKNIK MALAYSIA
Programming in C Functions PPT Presentation.pdf
programlama fonksiyonlar c++ hjhjghjv jg
Unit iv functions
Fp201 unit5 1

Similar to Lab 2 Math functions c++ programming.pptx (20)

PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PDF
Functions
PPT
Recursion in C
PPT
RECURSION IN C
PPT
function_v1fgdfdf5645ythyth6ythythgbg.ppt
PPT
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
PDF
Cpp functions
PPT
function_v1.ppt
PPT
function_v1.ppt
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
DOC
Functions
PPTX
CH.4FUNCTIONS IN C (1).pptx
PDF
unit3 part2 pcds function notes.pdf
PPTX
functioninpython-1.pptx
PPTX
Detailed concept of function in c programming
PPTX
unit_2 (1).pptx
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPTX
PDF
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Functions
Recursion in C
RECURSION IN C
function_v1fgdfdf5645ythyth6ythythgbg.ppt
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
Cpp functions
function_v1.ppt
function_v1.ppt
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Functions
CH.4FUNCTIONS IN C (1).pptx
unit3 part2 pcds function notes.pdf
functioninpython-1.pptx
Detailed concept of function in c programming
unit_2 (1).pptx
Chp8_C++_Functions_Part2_User-defined functions.pptx
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Ad

Recently uploaded (20)

PDF
Fluorescence-microscope_Botany_detailed content
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
1_Introduction to advance data techniques.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Computer network topology notes for revision
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Fluorescence-microscope_Botany_detailed content
Clinical guidelines as a resource for EBP(1).pdf
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
.pdf is not working space design for the following data for the following dat...
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
climate analysis of Dhaka ,Banglades.pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
1_Introduction to advance data techniques.pptx
Miokarditis (Inflamasi pada Otot Jantung)
Introduction-to-Cloud-ComputingFinal.pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
oil_refinery_comprehensive_20250804084928 (1).pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Computer network topology notes for revision
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Ad

Lab 2 Math functions c++ programming.pptx

  • 2. WHAT ARE FUNCTIONS? A function is a block of code that performs a specific task. Functions are used to call a formula in the main program It is not included in the main body Can be called number of times
  • 4. STANDARD FUNCTIONS These standard functions are groups in different libraries which can be included in the C++ program, e.g math.h, iostream.h
  • 5. USERDEFINEDFUNCTIONS Although C++ is shipped with a lot of standard functions, these functions are not enough for all users, therefore, C++ provides its users with a way to define their own functions (or user-defined function)
  • 6. DEFINING FUNCTIONS A C++ function consists of two parts The function header, and The function body The function header has the following syntax <return type> <name> (<parameter list>) The function body is simply a C++ code enclosed between { }
  • 7. EXAMPLE int sum(int a, int b){ int c = a+b; return c; }
  • 8. EXAMPLE- (CALLING A FUNCTION) int main(){ int num1, num2; cout<<"Enter first number"<<endl; cin>>num1; cout<<"Enter second number"<<endl; cin>>num2; cout<<"The sum is "<<sum(num1, num2); return 0; }
  • 9. FUNCTION PROTOTYPE A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program.
  • 10. SYNTAX OF FUNCTION PROTOTYPE returnType functionName(type1 argument1, type2 argument2, ...); Int sum (int a, int b) Declaration: int sum(int, int);
  • 11. PASSING ARGUMENTS TO A FUNCTION In programming, argument refers to the variable passed to the function. In the above example, two variables n1 and n2 are passed during the function call. The parameters a and b accepts the passed arguments in the function definition. These arguments are called formal parameters of the function.
  • 12. RETURNSTATEMENT The return statement terminates the execution of a function and returns a value to the calling function. The void function is used when no return value is needed
  • 13. EXAMPLE:FINDWHETHERANUMBERISEVENORODD void eo(int x) { if (x%2 == 0) cout << "Evenn"; else cout << "Oddn"; } int main() { eo(4); eo(5); return 0; }
  • 15. Void max( float [], int []); Void temp( float[][], int[][]); DECLARATIONOFAFUNCTIONWITHARRAYARGUMENTS
  • 16. PROGRAM TO FIND A NUMBER AND ITS POSITION IN AN ARRAY USING FUNCTIONS
  • 18. When a function is called, the values that are passed in the call are called arguments or actual parameters ( The actual parameters are x and y in this case) Formal parameters are local variables which are assigned values from the arguments when the function is called. ( The formal parameters are a and b in this case which are assigned the values of x and y, to perform the operation) int add( int a, int b); int main() { int x=3; y=4; add(x,y)} ACTUALANDFORMALPARAMETERS
  • 19. Call by value Call by reference TYPESOFFUNCTIONCALLS
  • 20. When the function is called in the main function, the values given to actual parameters are copied to formal parameters of the function and the operation is performed This means that the values assigned to actual parameters in the main function remain unchanged no matter whatever the operations are performed in the function POINTSTOREMEMEBER (CALLBYVALUE)
  • 21. In call of call by reference, the address is copied to the function using pointers This means that the value stored at the address in the main function can be changed using functions POINTSTOREMEMEBER (CALLBYREFERENCE)
  • 22. int add(int &a, int &b) { int temp; temp=a; a=b; b=temp; } (code in the notes below) CALLBYREFERENCE(SWAPEXAMPLE)
  • 23. CLASSTASK (MAHDEPT) Make a mathematical calculator using functions ( addition, subtraction, multiplication, division)
  • 24. 1. Get two values a and b, and change the values of a to a=a+b; and b=a-b; using call by reference 2. Define two functions to print the maximum and the minimum number respectively among three numbers entered by user. 3. Write a program to print the circumference and area of a circle of radius entered by user by defining your own function. HOMETASKS(MATHDEPT)

Editor's Notes

  • #16: #include<iostream> using namespace std; void find(int x, int arr[]) { for(int i=0; i<=4; i++) { if(arr[i]==x) cout<<"Number found at postion " << i+1; } } int main() { int array[4], num; cout<<"Enter the numbers in the array "; for ( int i=0; i<4; i++) { cin>>array[i]; } cout<<"Enter the number to find "; cin>>num; find(num, array); }
  • #22: #include<iostream> using namespace std; int add(int &a, int &b) { int temp; temp=a; a=b; b=temp; } int main() { int x=4; int y= 5; cout<<"The first values of x is =" <<x<<endl; cout<<"The first values of y is =" <<y<<endl; swap(&x,&y); cout<<"The second values of x is =" <<x<<endl; cout<<"The second values of y is =" <<y<<endl; }