SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (18)

03 function overloading
03 function overloading03 function overloading
03 function overloading
Jasleen Kaur (Chandigarh University)
 
C++ Function
C++ FunctionC++ Function
C++ Function
Hajar
 
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++
NUST Stuff
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
kavitha muneeshwaran
 
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++
Sachin Yadav
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Abdullah Turkistani
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
sathish sak
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Function C++
Function C++ Function C++
Function C++
Shahzad Afridi
 
C++ functions
C++ functionsC++ functions
C++ functions
Mayank Jain
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Nikhil Pandit
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 
Function overloading
Function overloadingFunction overloading
Function overloading
Selvin Josy Bai Somu
 
C++ Function
C++ FunctionC++ Function
C++ Function
PingLun Liao
 
Function & Recursion in C
Function & Recursion in CFunction & Recursion in C
Function & Recursion in C
Aditya Nihal Kumar Singh
 
C++ functions
C++ functionsC++ functions
C++ functions
Dawood Jutt
 

Viewers also liked (20)

The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
Christopher Gratton
 
Algorithms
AlgorithmsAlgorithms
Algorithms
Olga Fedoseeva
 
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 transformer
lnikolaeva
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Kamal Acharya
 
Secondary storage structure
Secondary storage structureSecondary storage structure
Secondary storage structure
Priya Selvaraj
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Secondary storage structure-Operating System Concepts
Secondary storage structure-Operating System ConceptsSecondary storage structure-Operating System Concepts
Secondary storage structure-Operating System Concepts
Arjun Kaimattathil
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
Mohammad Shaker
 
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
EugenioBrown1
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
Agnas Jasmine
 
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 Persian
Muhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
Qrator 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 Patil
widespreadpromotion
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Maaz Hasan
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
HNDE Labuduwa Galle
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
Wei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Imperva
 
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 transformer
lnikolaeva
 
Secondary storage structure
Secondary storage structureSecondary storage structure
Secondary storage structure
Priya Selvaraj
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Secondary storage structure-Operating System Concepts
Secondary storage structure-Operating System ConceptsSecondary storage structure-Operating System Concepts
Secondary storage structure-Operating System Concepts
Arjun 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 class
EugenioBrown1
 
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 Persian
Muhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
Qrator 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 Patil
widespreadpromotion
 
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 CPP
Wei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Imperva
 
Ad

Similar to Learning C++ - Functions in C++ 3 (20)

Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
Venkateswarlu Vuggam
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Function
FunctionFunction
Function
yash patel
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
Koteswari Kasireddy
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
DikshaDani5
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
Chapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptxChapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvdFunctions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
scs150831
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
1.6 Function.pdf
1.6 Function.pdf1.6 Function.pdf
1.6 Function.pdf
NirmalaShinde3
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
Emertxe Information Technologies Pvt Ltd
 
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
estorebackupr
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Inbuilt Functions in C++ computer language.ppt
Inbuilt Functions in C++ computer language.pptInbuilt Functions in C++ computer language.ppt
Inbuilt Functions in C++ computer language.ppt
AjayLobo1
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
C++ Functions.pptx
C++ Functions.pptxC++ Functions.pptx
C++ Functions.pptx
DikshaDani5
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
Chapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptxChapter 1 (2) array and structure r.pptx
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvdFunctions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
scs150831
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
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
estorebackupr
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Inbuilt Functions in C++ computer language.ppt
Inbuilt Functions in C++ computer language.pptInbuilt Functions in C++ computer language.ppt
Inbuilt Functions in C++ computer language.ppt
AjayLobo1
 
Ad

Recently uploaded (20)

Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Prediction of Flexural Strength of Concrete Produced by Using Pozzolanic Mate...
Journal of Soft Computing in Civil Engineering
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 

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
  翻译: