SlideShare une entreprise Scribd logo
1  sur  18
Functions in C++
1
Section Outline
• Definition of a function
• Calling function
• Recursive functions
2
Section Outline
• Definition of a function
• Calling function
• Recursive functions
3
Definition of a function
• Every where in C++ environment we can
define a function
OutPutType name (argument1, argument2, …)
{
…………………………
…………………………
…………………………
return output
}
4
This two type must have a
same type
Definition of a function(cont.)
• Some note:
Functions in C++ works the same as SUBROUTINs
There is no need to use CONTAINS or any similar
keywords to introduce functions to the program
The main() part of our code must know the
presence of our functions and this possible by a
predefine or define our function the top of main()
(disobey of this rule make a error)
5
Definition of a function(cont.)
• e.g. (1) : predefine
int MyFuntioin(int);
int main()
{
int t = 12;
int a = MyFunction(t);
return 0;
}
int MyFunction(int q)
{
……..
……..
……..
return f;
}
6
• e.g. (2): define
int MyFunction(int q)
{
……..
……..
……..
return f;
}
int main()
{
int t = 12;
int a = MyFunction(t);
return 0;
}
Definition of a function(cont.)
• Some notes:
 We can use function`s output like a statement or a variable
• Anywhere in our code, we can define a function such as MyFunction ()
that it`s output is an integer. after that we write the following
command to use the function:
int a = MyFunction();
e.g.
int sub (int a, int b) { return a-b; }
void main() { int a = sub (8,2); return; }
Note: There is no need to use CALL or such key word to call a function,
we just need to write function’s name.
7
Definition of a function(cont.)
• Some notes(cont.):
If you want to write your own functions into the
other files or headers (instead of the main file),
you must include them just like libraries with “..”:
e.g:
#include “myfunctions.h”
8
Definition of a function(cont.)
• e.g.(.h and .cpp)
# include “sub.h”
void main()
{
int a = sub(7,2);
cout << a;
}
Note : we use “…” in define include when we create
a header ourselves
9
//sub.h
int sub (int , int );
//sub.cpp
# include ”sub.h”
int sub (int a, int b)
{
return a-b;
}
Section Outline
• Definition of a function
• Calling Function
• Recursive functions
10
Calling Function
• Passing parameter
– e.g. int sub (int a, int b, int c)
{ return b-c; }
void main ()
{ cout <<sub(e,g,f); return 0; }
– Arrays
• Because of array`s nature as pointer, we pass an array as a pointer
• Upper dimensions than 1D must introduce to the list of argument
(we can introduce all dimensions to the function)
11
Calling Function(cont.)
• e.g.
int array _based _function ( int * first , int second[][5])
{…………………….}
int main ()
{
int a[100], b[100][5];
return array _based _function (a, b);
}
12
int first[]int first[100]
Calling Function(cont.)
• We have two types of calling function
1. Calling by value
2. Calling by references
13
Calling Function(cont.)
1. Calling by value
e.g.
int cubeValue(int n)
{
return n*n*n;
}
int main()
{
int number = 5;
number = cubeValue(5);
return 0;
}
14
int cubeValue ( int n)
{
int f = n*n*n;
return f;
}
Calling Function(cont.)
2. Calling by references
e.g.
void cubeValue(int * n)
{
*n = *n * *n * *n;
}
int main()
{
int number = 5;
number = cubeValue(& number);
return 0;
}
15
Some Important Functions
• List of some computational functions:
• Note: You must include the math library in order to use them.
#include <math>
16
Mathematical def. C++ Fortran
sqrt(x) SQRT(X)
exp(x) exp(x) EXP(X)
ln(x) ln(x) LOG(X)
log10(x) LOG10(X)
sin(x) sin(x) SIN(X)
cos(x) cos(x) COS(X)
tan(x) tan(x) TAN(X)
Residual x % y MOD(X,Y)
x
10logx
Some Important Functions
#include <math>
17
Mathematical def. C++ Fortran
sinh(x) sinh(x) SINH(X)
cosh(x) cosh(x) COSH(X)
tanh(x) tanh(x) TANH(X)
sin-1(x) asin(x) ASIN(X)
cos-1(x) acos(x) ACOS(X)
tan-1(x) atan(x) ATAN(X)
|x| labs(x) ABS(X)
[x] floor(x) INT(X)
In future :
i. Class and related concepts
ii. ERRORS
18

Contenu connexe

Tendances (18)

03 function overloading
03 function overloading03 function overloading
03 function overloading
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Function C++
Function C++ Function C++
Function C++
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Inline function
Inline functionInline function
Inline function
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
 
C++ functions
C++ functionsC++ functions
C++ functions
 

En vedette

Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods kinan keshkeh
 
Recursion transformer
Recursion transformerRecursion transformer
Recursion transformerlnikolaeva
 
Secondary storage structure
Secondary storage structureSecondary storage structure
Secondary storage structurePriya Selvaraj
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 
Secondary storage structure-Operating System Concepts
Secondary storage structure-Operating System ConceptsSecondary storage structure-Operating System Concepts
Secondary storage structure-Operating System ConceptsArjun Kaimattathil
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classEugenioBrown1
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروعziadalmulla
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianMuhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016Qrator Labs
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPPWei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 

En vedette (20)

The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods Simpson and lagranje dalambair math methods
Simpson and lagranje dalambair math methods
 
Recursion transformer
Recursion transformerRecursion transformer
Recursion transformer
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Secondary storage structure
Secondary storage structureSecondary storage structure
Secondary storage structure
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Secondary storage structure-Operating System Concepts
Secondary storage structure-Operating System ConceptsSecondary storage structure-Operating System Concepts
Secondary storage structure-Operating System Concepts
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire class
 
C++
C++C++
C++
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروع
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in Persian
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similaire à Learning C++ - Functions in C++ 3

Similaire à Learning C++ - Functions in C++ 3 (20)

Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Function
FunctionFunction
Function
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Function in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programmingFunction in C++, Methods in C++ coding programming
Function in C++, Methods in C++ coding programming
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Functions
Functions Functions
Functions
 

Dernier

Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxchumtiyababu
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxMuhammadAsimMuhammad6
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 

Dernier (20)

Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Verification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptxVerification of thevenin's theorem for BEEE Lab (1).pptx
Verification of thevenin's theorem for BEEE Lab (1).pptx
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 

Learning C++ - Functions in C++ 3

  • 2. Section Outline • Definition of a function • Calling function • Recursive functions 2
  • 3. Section Outline • Definition of a function • Calling function • Recursive functions 3
  • 4. Definition of a function • Every where in C++ environment we can define a function OutPutType name (argument1, argument2, …) { ………………………… ………………………… ………………………… return output } 4 This two type must have a same type
  • 5. Definition of a function(cont.) • Some note: Functions in C++ works the same as SUBROUTINs There is no need to use CONTAINS or any similar keywords to introduce functions to the program The main() part of our code must know the presence of our functions and this possible by a predefine or define our function the top of main() (disobey of this rule make a error) 5
  • 6. Definition of a function(cont.) • e.g. (1) : predefine int MyFuntioin(int); int main() { int t = 12; int a = MyFunction(t); return 0; } int MyFunction(int q) { …….. …….. …….. return f; } 6 • e.g. (2): define int MyFunction(int q) { …….. …….. …….. return f; } int main() { int t = 12; int a = MyFunction(t); return 0; }
  • 7. Definition of a function(cont.) • Some notes:  We can use function`s output like a statement or a variable • Anywhere in our code, we can define a function such as MyFunction () that it`s output is an integer. after that we write the following command to use the function: int a = MyFunction(); e.g. int sub (int a, int b) { return a-b; } void main() { int a = sub (8,2); return; } Note: There is no need to use CALL or such key word to call a function, we just need to write function’s name. 7
  • 8. Definition of a function(cont.) • Some notes(cont.): If you want to write your own functions into the other files or headers (instead of the main file), you must include them just like libraries with “..”: e.g: #include “myfunctions.h” 8
  • 9. Definition of a function(cont.) • e.g.(.h and .cpp) # include “sub.h” void main() { int a = sub(7,2); cout << a; } Note : we use “…” in define include when we create a header ourselves 9 //sub.h int sub (int , int ); //sub.cpp # include ”sub.h” int sub (int a, int b) { return a-b; }
  • 10. Section Outline • Definition of a function • Calling Function • Recursive functions 10
  • 11. Calling Function • Passing parameter – e.g. int sub (int a, int b, int c) { return b-c; } void main () { cout <<sub(e,g,f); return 0; } – Arrays • Because of array`s nature as pointer, we pass an array as a pointer • Upper dimensions than 1D must introduce to the list of argument (we can introduce all dimensions to the function) 11
  • 12. Calling Function(cont.) • e.g. int array _based _function ( int * first , int second[][5]) {…………………….} int main () { int a[100], b[100][5]; return array _based _function (a, b); } 12 int first[]int first[100]
  • 13. Calling Function(cont.) • We have two types of calling function 1. Calling by value 2. Calling by references 13
  • 14. Calling Function(cont.) 1. Calling by value e.g. int cubeValue(int n) { return n*n*n; } int main() { int number = 5; number = cubeValue(5); return 0; } 14 int cubeValue ( int n) { int f = n*n*n; return f; }
  • 15. Calling Function(cont.) 2. Calling by references e.g. void cubeValue(int * n) { *n = *n * *n * *n; } int main() { int number = 5; number = cubeValue(& number); return 0; } 15
  • 16. Some Important Functions • List of some computational functions: • Note: You must include the math library in order to use them. #include <math> 16 Mathematical def. C++ Fortran sqrt(x) SQRT(X) exp(x) exp(x) EXP(X) ln(x) ln(x) LOG(X) log10(x) LOG10(X) sin(x) sin(x) SIN(X) cos(x) cos(x) COS(X) tan(x) tan(x) TAN(X) Residual x % y MOD(X,Y) x 10logx
  • 17. Some Important Functions #include <math> 17 Mathematical def. C++ Fortran sinh(x) sinh(x) SINH(X) cosh(x) cosh(x) COSH(X) tanh(x) tanh(x) TANH(X) sin-1(x) asin(x) ASIN(X) cos-1(x) acos(x) ACOS(X) tan-1(x) atan(x) ATAN(X) |x| labs(x) ABS(X) [x] floor(x) INT(X)
  • 18. In future : i. Class and related concepts ii. ERRORS 18