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

More Related Content

What's hot (17)

PDF
03 function overloading
Jasleen Kaur (Chandigarh University)
 
PPT
C++ Function
Hajar
 
PPT
Lecture#7 Call by value and reference in c++
NUST Stuff
 
PPT
Functions in C++
Mohammed Sikander
 
PPTX
C and C++ functions
kavitha muneeshwaran
 
PPTX
Call by value or call by reference in C++
Sachin Yadav
 
PPTX
Functions in C++
home
 
PPT
Functions in c++
Abdullah Turkistani
 
PPT
C++ Functions
sathish sak
 
PPT
16717 functions in C++
LPU
 
PPTX
Function C++
Shahzad Afridi
 
PPT
C++ functions
Mayank Jain
 
PPT
Functions in C++
Nikhil Pandit
 
PPTX
Inline function
Tech_MX
 
PPTX
Function overloading
Selvin Josy Bai Somu
 
ODP
C++ Function
PingLun Liao
 
PPT
C++ functions
Dawood Jutt
 
03 function overloading
Jasleen Kaur (Chandigarh University)
 
C++ Function
Hajar
 
Lecture#7 Call by value and reference in c++
NUST Stuff
 
Functions in C++
Mohammed Sikander
 
C and C++ functions
kavitha muneeshwaran
 
Call by value or call by reference in C++
Sachin Yadav
 
Functions in C++
home
 
Functions in c++
Abdullah Turkistani
 
C++ Functions
sathish sak
 
16717 functions in C++
LPU
 
Function C++
Shahzad Afridi
 
C++ functions
Mayank Jain
 
Functions in C++
Nikhil Pandit
 
Inline function
Tech_MX
 
Function overloading
Selvin Josy Bai Somu
 
C++ Function
PingLun Liao
 
C++ functions
Dawood Jutt
 

Viewers also liked (20)

DOCX
The Algebra of Functions
Christopher Gratton
 
ODP
Algorithms
Olga Fedoseeva
 
PDF
Simpson and lagranje dalambair math methods
kinan keshkeh
 
PPTX
Recursion transformer
lnikolaeva
 
PPTX
Recursive Function
Kamal Acharya
 
PPTX
Secondary storage structure
Priya Selvaraj
 
PPTX
Union in C programming
Kamal Acharya
 
PPT
Secondary storage structure-Operating System Concepts
Arjun Kaimattathil
 
PDF
C++ L05-Functions
Mohammad Shaker
 
PDF
Netw450 advanced network security with lab entire class
EugenioBrown1
 
PPT
Disk scheduling
Agnas Jasmine
 
PPTX
Functions c++ مشروع
ziadalmulla
 
PDF
Network-security muhibullah aman-first edition-in Persian
Muhibullah Aman
 
PDF
Network Security in 2016
Qrator Labs
 
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPT
Functions in c++
Maaz Hasan
 
PPTX
C++ lecture 03
HNDE Labuduwa Galle
 
PDF
Basic openCV Functions Using CPP
Wei-Wen Hsu
 
PPTX
Functions in C++
home
 
PDF
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Imperva
 
The Algebra of Functions
Christopher Gratton
 
Algorithms
Olga Fedoseeva
 
Simpson and lagranje dalambair math methods
kinan keshkeh
 
Recursion transformer
lnikolaeva
 
Recursive Function
Kamal Acharya
 
Secondary storage structure
Priya Selvaraj
 
Union in C programming
Kamal Acharya
 
Secondary storage structure-Operating System Concepts
Arjun Kaimattathil
 
C++ L05-Functions
Mohammad Shaker
 
Netw450 advanced network security with lab entire class
EugenioBrown1
 
Disk scheduling
Agnas Jasmine
 
Functions c++ مشروع
ziadalmulla
 
Network-security muhibullah aman-first edition-in Persian
Muhibullah Aman
 
Network Security in 2016
Qrator Labs
 
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Functions in c++
Maaz Hasan
 
C++ lecture 03
HNDE Labuduwa Galle
 
Basic openCV Functions Using CPP
Wei-Wen Hsu
 
Functions in C++
home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Imperva
 
Ad

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

PPTX
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
PPTX
C++_Functions_Detailed_Presentation.pptx
umerchegy
 
PDF
(3) cpp procedural programming
Nico Ludwig
 
PPTX
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
PPTX
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
PPT
C++ Functions.ppt
kanaka vardhini
 
PPT
power point presentation on object oriented programming functions concepts
bhargavi804095
 
PPT
user defined function
King Kavin Patel
 
PPTX
C functions
University of Potsdam
 
PPT
POLITEKNIK MALAYSIA
Aiman Hud
 
PDF
Pro
TeshaleSiyum
 
PPTX
Functions.pptx
AkshayKumarK14
 
PPTX
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
PPTX
C++ Functions.pptx
DikshaDani5
 
PPTX
Fundamental of programming Fundamental of programming
LidetAdmassu
 
PDF
Cpp functions
NabeelaNousheen
 
PPT
C++ functions
Dawood Jutt
 
PDF
Chapter 5 - Modular Programming.pdf
KirubelWondwoson1
 
PDF
Functions
Pragnavi Erva
 
Chapter 1 (2) array and structure r.pptx
abenezertekalign118
 
C++_Functions_Detailed_Presentation.pptx
umerchegy
 
(3) cpp procedural programming
Nico Ludwig
 
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
6. Functions in C ++ programming object oriented programming
Ahmad177077
 
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
jacobdiriba
 
C++ Functions.ppt
kanaka vardhini
 
power point presentation on object oriented programming functions concepts
bhargavi804095
 
user defined function
King Kavin Patel
 
POLITEKNIK MALAYSIA
Aiman Hud
 
Functions.pptx
AkshayKumarK14
 
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
C++ Functions.pptx
DikshaDani5
 
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Cpp functions
NabeelaNousheen
 
C++ functions
Dawood Jutt
 
Chapter 5 - Modular Programming.pdf
KirubelWondwoson1
 
Functions
Pragnavi Erva
 
Ad

Recently uploaded (20)

PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPT
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Carmon_Remote Sensing GIS by Mahesh kumar
DhananjayM6
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Day2 B2 Best.pptx
helenjenefa1
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 

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