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

PPT
Functions in C++
PPT
C++ functions presentation by DHEERAJ KATARIA
PPT
Lecture#6 functions in c++
PPTX
Functions in C++ (OOP)
PPTX
C++ programming function
PPTX
functions of C++
PPT
Function overloading(C++)
PPT
FUNCTIONS IN c++ PPT
Functions in C++
C++ functions presentation by DHEERAJ KATARIA
Lecture#6 functions in c++
Functions in C++ (OOP)
C++ programming function
functions of C++
Function overloading(C++)
FUNCTIONS IN c++ PPT

What's hot (17)

PDF
03 function overloading
PPT
C++ Function
PPT
Lecture#7 Call by value and reference in c++
PPT
Functions in C++
PPTX
C and C++ functions
PPTX
Call by value or call by reference in C++
PPTX
Functions in C++
PPT
Functions in c++
PPT
C++ Functions
PPT
16717 functions in C++
 
PPTX
Function C++
PPT
C++ functions
PPT
Functions in C++
PPTX
Inline function
PPTX
Function overloading
ODP
C++ Function
PPT
C++ functions
03 function overloading
C++ Function
Lecture#7 Call by value and reference in c++
Functions in C++
C and C++ functions
Call by value or call by reference in C++
Functions in C++
Functions in c++
C++ Functions
16717 functions in C++
 
Function C++
C++ functions
Functions in C++
Inline function
Function overloading
C++ Function
C++ functions
Ad

Viewers also liked (20)

DOCX
The Algebra of Functions
ODP
Algorithms
PDF
Simpson and lagranje dalambair math methods
PPTX
Recursion transformer
PPTX
Recursive Function
PPTX
Secondary storage structure
PPTX
Union in C programming
PPT
Secondary storage structure-Operating System Concepts
PDF
C++ L05-Functions
PDF
Netw450 advanced network security with lab entire class
PPT
Disk scheduling
PPTX
Functions c++ مشروع
PDF
Network-security muhibullah aman-first edition-in Persian
PDF
Network Security in 2016
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
PPT
Functions in c++
PPTX
C++ lecture 03
PDF
Basic openCV Functions Using CPP
PPTX
Functions in C++
PDF
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
The Algebra of Functions
Algorithms
Simpson and lagranje dalambair math methods
Recursion transformer
Recursive Function
Secondary storage structure
Union in C programming
Secondary storage structure-Operating System Concepts
C++ L05-Functions
Netw450 advanced network security with lab entire class
Disk scheduling
Functions c++ مشروع
Network-security muhibullah aman-first edition-in Persian
Network Security in 2016
4. Recursion - Data Structures using C++ by Varsha Patil
Functions in c++
C++ lecture 03
Basic openCV Functions Using CPP
Functions in C++
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Ad

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

PPTX
Chapter 1 (2) array and structure r.pptx
PPTX
C++_Functions_Detailed_Presentation.pptx
PDF
(3) cpp procedural programming
PPTX
Part 3-functions1-120315220356-phpapp01
PPTX
6. Functions in C ++ programming object oriented programming
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PPT
C++ Functions.ppt
PPT
power point presentation on object oriented programming functions concepts
PPT
user defined function
PPTX
PPT
POLITEKNIK MALAYSIA
PDF
PPTX
Functions.pptx
PPTX
CHAPTER THREE FUNCTION.pptx
PPTX
C++ Functions.pptx
PPTX
Fundamental of programming Fundamental of programming
PDF
Cpp functions
PPT
C++ functions
PDF
Chapter 5 - Modular Programming.pdf
PDF
Functions
Chapter 1 (2) array and structure r.pptx
C++_Functions_Detailed_Presentation.pptx
(3) cpp procedural programming
Part 3-functions1-120315220356-phpapp01
6. Functions in C ++ programming object oriented programming
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
C++ Functions.ppt
power point presentation on object oriented programming functions concepts
user defined function
POLITEKNIK MALAYSIA
Functions.pptx
CHAPTER THREE FUNCTION.pptx
C++ Functions.pptx
Fundamental of programming Fundamental of programming
Cpp functions
C++ functions
Chapter 5 - Modular Programming.pdf
Functions

Recently uploaded (20)

PDF
composite construction of structures.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Sustainable Sites - Green Building Construction
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Welding lecture in detail for understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT
Project quality management in manufacturing
composite construction of structures.pdf
bas. eng. economics group 4 presentation 1.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Sustainable Sites - Green Building Construction
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Digital Logic Computer Design lecture notes
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Welding lecture in detail for understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Project quality management in manufacturing

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