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)

PPTX
436813905-LNG-Process-Overview-Short.pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPT
Project quality management in manufacturing
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Geodesy 1.pptx...............................................
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
composite construction of structures.pdf
PPTX
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Practice Questions on recent development part 1.pptx
PPT
Drone Technology Electronics components_1
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPTX
Internship_Presentation_Final engineering.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
436813905-LNG-Process-Overview-Short.pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Project quality management in manufacturing
Queuing formulas to evaluate throughputs and servers
OOP with Java - Java Introduction (Basics)
Geodesy 1.pptx...............................................
Road Safety tips for School Kids by a k maurya.pptx
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Simulation of electric circuit laws using tinkercad.pptx
composite construction of structures.pdf
The-Looming-Shadow-How-AI-Poses-Dangers-to-Humanity.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Practice Questions on recent development part 1.pptx
Drone Technology Electronics components_1
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Internship_Presentation_Final engineering.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT

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