SlideShare a Scribd company logo
Kinan keshkeh 
IT Engineering-Damascus University 
3rd year 
Summer course- 2014 
2 bytes team
• 
Welcome again ! 
• 
Who has any Ques? 
• 
So Let’s Go ! 
• 
What we will take : 
• 
Functions. 
• 
parameters& overloading
Function Basics!
Functions 
•Two ways to write functions in your program: 
•Declaration / calling / definition
Functions 
•Two ways to write functions in your program: 
•Declaration / calling / definition 
Before main 
Inside main 
After main
Functions 
•Two ways to write functions in your program: 
•Declaration / calling / definition 
•Declaration + definition / calling
Functions 
•Declaration / calling / definition 
#include<iostream> 
using namespace std ; 
Float avg(float x, float y ) ; 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
float avg(float x, float y ) 
{ 
return (x+y)/2; 
}
#include<iostream> 
using namespace std ; 
Float avg(float x, float y ) ; 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
float avg(float x, float y ) 
{ 
return (x+y)/2; 
} 
Functions 
•Declaration / calling / definition 
Declaration
#include<iostream> using namespace std ; Float avg(float x, float y ) ; int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avg : "<<c<<endl; system("pause"); return 0; } float avg(float x, float y ) { return (x+y)/2; } 
Functions 
•Declaration / calling / definition 
Declaration 
calling
#include<iostream> 
using namespace std ; 
Float avg(float x, float y ) ; 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
float avg(float x, float y ) 
{ 
return (x+y)/2; 
} 
Functions 
•Declaration / calling / definition 
Declaration 
calling 
Definition
Functions 
•Declaration + definition / calling 
#include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; }
Functions 
•Declaration + definition / calling 
#include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } 
Declaration+ definition
Functions 
•Declaration + definition / calling 
#include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } 
Declaration+ definition 
calling
Functions 
#include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } 
Returned value
Functions 
#include<iostream> 
using namespace std ; 
float avg(float x , float y ) 
{ 
return (x+y)/2; 
} 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avssg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
It could be Void , then no return and no “ = ”
Functions 
#include<iostream> 
using namespace std ; 
float avg(float x , float y ) 
{ 
return (x+y)/2; 
} 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avssg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
Attention !! 
“ , ” Not “ ; ”
Functions 
#include<iostream> 
using namespace std ; 
float avg(float x , float y ) 
{ 
return (x+y)/2; 
} 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avssg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
Variables like in procedures & functions in Pascal !
Functions 
#include<iostream> 
using namespace std ; 
float avg(float x , float y ) 
{ 
return (x+y)/2; 
} 
int main () 
{ 
float x,y; 
cout<<" Enter the first number: n"; 
cin>>x; 
cout<<" Enter the second number: n"; 
cin>>y; 
float c=avg(x,y); 
cout<<"the avssg : "<<c<<endl; 
system("pause"); 
return 0; 
} 
Also , it could be null 
Float avg()
Predefined Functions !
Predefined Functions 
•It’s existing in libraries in C++ 
•You can call it without writing any definition or declaration !! 
•You have to put using namespace std with all functions !
Predefined Functions 
Name 
Description 
Type of 
Arguments 
Type of 
Value 
Example 
Value 
Library 
Header 
sqrt 
Square root 
double 
double 
sqrt(4.0) 
2.0 
cmath 
pow 
Powers 
double 
double 
pow(2.0,3.0) 
8.0 
cmath 
abs 
Absolute value for int 
int 
int 
abs(-7) 
abs(7) 
7 
7 
cstdlib 
labs 
Absolute value for long 
long 
long 
labs(-70000) labs(70000) 
70000 70000 
cstdlib 
fabs 
Absolute value for double 
double 
double 
fabs(-7.5) fabs(7.5) 
7.5 
7.5 
cmath 
ceil 
Ceiling (round up) 
double 
double 
ceil(3.2) ceil(3.9) 
4.0 
4.0 
cmath 
floor 
Floor (round down) 
double 
double 
floor(3.2) floor(3.9) 
3.0 
3.0 
cmath 
exit 
End program 
int 
void 
exit(1); 
None 
cstdlib 
rand 
Random number 
None 
int 
rand( ) 
Varies 
cstdlib 
srand 
Set seed for rand 
unsigned int 
void 
srand(42); 
None 
cstdlib
Predefined Functions 
Name 
Description 
Type of 
Arguments 
Type of 
Value 
Example 
Value 
Library 
Header 
sqrt 
Square root 
double 
double 
sqrt(4.0) 
2.0 
cmath 
pow 
Powers 
double 
double 
pow(2.0,3.0) 
8.0 
cmath 
abs 
Absolute value for int 
int 
int 
abs (-7) 
abs(7) 
7 
7 
cstdlib 
labs 
Absolute value for long 
long 
long 
labs(-70000) labs(70000) 
70000 70000 
cstdlib 
fabs 
Absolute value for double 
double 
double 
fabs(-7.5) fabs(7.5) 
7.5 
7.5 
cmath 
ceil 
Ceiling (round up) 
double 
double 
ceil(3.2) ceil(3.9) 
4.0 
4.0 
cmath 
floor 
Floor (round down) 
double 
double 
floor(3.2) floor(3.9) 
3.0 
3.0 
cmath 
exit 
End program 
int 
void 
exit(1); 
None 
cstdlib 
rand 
Random number 
None 
int 
rand( ) 
Varies 
cstdlib 
srand 
Set seed for rand 
unsigned int 
void 
srand (42); 
None 
cstdlib
///Computes the size of a doghouse that can be purchased //given the user’s budget. #include <iostream> #include <cmath> using namespace std; int main( ){ const double COST_PER_SQ_FT = 10.50; //cost per square feet double budget, area, lengthSide; cout << "Enter the amount budgeted for your doghouse $"; cin >> budget; area = budget/COST_PER_SQ_FT; lengthSide = sqrt(area); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "For a price of $" << budget << endl << "I can build you a luxurious square doghousen" << "that is " << lengthSide << " feet on each side.n"; return 0; } 
Predefined Functions
#include <iostream> 
#include <cstdlib> 
using namespace std; 
int main( ){ 
int month, day; 
cout << "Welcome to your friendly weather program.n" 
<< "Enter today’s date as two integers for the month and the day:n"; 
cin >> month; 
cin >> day; 
srand(month*day); //using seed number=month*day 
int prediction; 
char ans; 
cout << "Weather for today:n"; 
do{ 
prediction = rand( ) % 3 ;// Scaling 
switch (prediction){ 
case 0: 
cout << "The day will be sunny!!n"; 
break; 
case 1: 
cout << "The day will be cloudy.n"; 
break; 
case 2: 
cout << "The day will be stormy!n"; 
break; 
default: 
cout << "Weather program is not functioning properly.n"; 
} 
cout << "Want the weather for the next day?(y/n): "; 
cin >> ans; 
} while (ans == 'y' || ans == 'Y'); 
cout << "That's it from your 24-hour weather program.n"; 
Return 0 ; 
} 
Code discussion 3
Pseudorandom Number Generator 
Code discussion 3 
The code does …
Local & global Variables !
Local & global Variables 
•Local Variables 
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . 
•You can use the same name of variables in different scopes ! 
#include<iostream> 
using namespace std ; 
int main (){ 
float x; 
cout<<" Enter the number: n"; 
cin>>x; 
{ 
float x=22; 
} 
cout<<"the number x : "<<x<<endl; 
system("pause"); return 0; 
} 
Block!
Local & global Variables 
•Local Variables 
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . 
•You can use the same name of variables in different scopes ! 
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
Local & global Variables 
•Local Variables 
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . 
•You can use the same name of variables in different scopes ! 
#include<iostream> 
using namespace std ; 
int main (){ 
float x; 
cout<<" Enter the number: n"; 
cin>>x; 
{ 
float x=22; 
} 
cout<<"the number x : "<<x<<endl; 
system("pause"); return 0; 
}
Local & global Variables 
•Local Variables 
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . 
•You can use the same name of variables in different scopes ! 
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
Local & global Variables 
•Local Variables 
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . 
•You can use the same name of variables in different scopes ! 
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
Local & global Variables 
•Local Variables 
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . 
•You can use the same name of variables in different scopes ! 
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
•Local Variables
Local & global Variables 
•Global Variables 
•At the first of program ( after “using namespace std” ) 
•It’s scope , all ‘blocks’ 
•If you have global variable (name x ) and other local variable ( name x too ) 
•If you want global x , you will write ::x
•Global Variables
Parameters and Overloading
Calling by value & by reference
Calling by value & by reference
Calling by value & by reference 
Without var 
With var
Calling by value & by reference 
Without var 
With var 
But “ & ” instead of var .
•Calling by value
•Calling by value 
•The Output :
•Calling by reference
•Calling by reference
•Calling by reference 
•The Output :
Overloading !
Overloading 
•The same functions names ,but different meanings 
•Returned_value Fname ( int v1 , double v1 …. ) 
•Returned_value Fname ( double v1 , double v1 …. ) 
•Returned_value Fname ( char v1 , double v1 …. )
Overloading 
•The same functions names ,but different meanings 
•Returned_value Fname ( int v1 , double v1 …. ) 
•Returned_value Fname ( double v1 , double v1 …. ) 
•Returned_value Fname ( char v1 , double v1 …. ) 
Difference here 
At parameters !
Overloading 
•The same functions names ,but different meanings 
•Returned_value Fname ( int v1 , double v1 …. ) 
•Returned_value Fname ( double v1 , double v1 …. ) 
•Returned_value Fname ( char v1 , double v1 …. ) 
Difference here At parameters ! 
In numbers of them , and in their kinds
Overloading 
•The same functions names ,but different meanings 
•Returned_value Fname ( int v1 , double v1 …. ) 
•Returned_value Fname ( double v1 , double v1 …. ) 
•Returned_value Fname ( char v1 , double v1 …. ) 
Difference here At parameters ! 
In numbers of them , and in their kinds 
Not here at returned value !
Overloading 
•The same functions names ,but different meanings 
•Returned_value Fname ( int v1 , double v1 …. ) 
•Returned_value Fname ( double v1 , double v1 …. ) 
•Returned_value Fname ( char v1 , double v1 …. ) 
•You will see it on operators + , - , * ….
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
Overloading
Overloading
Overloading
•Default Arguments
Homework 
•Write a program that calls a function , in function the user input a Float number (ex: 3.6) , then the output is “ (3) and (0.6) “ 
•Write a program that calls a Recursive function , in function the user input an integer number (ex: 3) , then the output is “ (3! = 6) “
That’s for today 
That’s for today guys !!
That’s for today 
Go and Play !
2 bytes team 
Group : group link 
Mobile phone- Kinan : 0994385748 
Facebook account : kinan’s account 
2 bytes team

More Related Content

What's hot (20)

PDF
c programming
Arun Umrao
 
PDF
NS2: AWK and GNUplot - PArt III
Ajit Nayak
 
PDF
Recursion to iteration automation.
Russell Childs
 
PDF
c programming
Arun Umrao
 
PDF
C++ L08-Classes Part1
Mohammad Shaker
 
PDF
jq: JSON - Like a Boss
Bob Tiernay
 
PDF
Ns2: Introduction - Part I
Ajit Nayak
 
PPTX
Academy PRO: ES2015
Binary Studio
 
PDF
Ns2: OTCL - PArt II
Ajit Nayak
 
PDF
Introduction to JQ
Knoldus Inc.
 
PPTX
Unit 3
GOWSIKRAJAP
 
PDF
Numerical Methods with Computer Programming
Utsav Patel
 
PDF
Programmation fonctionnelle en JavaScript
Loïc Knuchel
 
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
PPTX
Groovy grails types, operators, objects
Husain Dalal
 
PDF
6. Generics. Collections. Streams
DEVTYPE
 
PDF
Javascript
Vlad Ifrim
 
PDF
Леонид Шевцов «Clojure в деле»
DataArt
 
PDF
Implementing Software Machines in C and Go
Eleanor McHugh
 
PPTX
Basic C++ 11/14 for Python Programmers
Appier
 
c programming
Arun Umrao
 
NS2: AWK and GNUplot - PArt III
Ajit Nayak
 
Recursion to iteration automation.
Russell Childs
 
c programming
Arun Umrao
 
C++ L08-Classes Part1
Mohammad Shaker
 
jq: JSON - Like a Boss
Bob Tiernay
 
Ns2: Introduction - Part I
Ajit Nayak
 
Academy PRO: ES2015
Binary Studio
 
Ns2: OTCL - PArt II
Ajit Nayak
 
Introduction to JQ
Knoldus Inc.
 
Unit 3
GOWSIKRAJAP
 
Numerical Methods with Computer Programming
Utsav Patel
 
Programmation fonctionnelle en JavaScript
Loïc Knuchel
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
Groovy grails types, operators, objects
Husain Dalal
 
6. Generics. Collections. Streams
DEVTYPE
 
Javascript
Vlad Ifrim
 
Леонид Шевцов «Clojure в деле»
DataArt
 
Implementing Software Machines in C and Go
Eleanor McHugh
 
Basic C++ 11/14 for Python Programmers
Appier
 

Viewers also liked (20)

PDF
Function lecture
DIT University, Dehradun
 
ODP
C++ Function
PingLun Liao
 
PPT
Pre defined Functions in C
Prabhu Govind
 
PPTX
Function in c++
Kumar
 
PPTX
Call by value
Dharani G
 
PPTX
C functions
University of Potsdam
 
PPT
Lovely Professional University Distance Education
Aastha Groups
 
PPT
16717 functions in C++
LPU
 
PPTX
C programming - String
Achyut Devkota
 
PPT
Functions
Online
 
PPT
C++ Function
Hajar
 
PPTX
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
PPT
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
DOC
String in c
Suneel Dogra
 
PPTX
Function in c
Raj Tandukar
 
PDF
Function in C
Dr. Abhineet Anand
 
PPTX
functions of C++
tarandeep_kaur
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPTX
Organizing (Management Functions)
Christian de la Cruz
 
PPTX
Functionsbsitact
Juan Apolinario Reyes
 
Function lecture
DIT University, Dehradun
 
C++ Function
PingLun Liao
 
Pre defined Functions in C
Prabhu Govind
 
Function in c++
Kumar
 
Call by value
Dharani G
 
Lovely Professional University Distance Education
Aastha Groups
 
16717 functions in C++
LPU
 
C programming - String
Achyut Devkota
 
Functions
Online
 
C++ Function
Hajar
 
Parameter passing to_functions_in_c
ForwardBlog Enewzletter
 
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
String in c
Suneel Dogra
 
Function in c
Raj Tandukar
 
Function in C
Dr. Abhineet Anand
 
functions of C++
tarandeep_kaur
 
Function in C program
Nurul Zakiah Zamri Tan
 
Organizing (Management Functions)
Christian de la Cruz
 
Functionsbsitact
Juan Apolinario Reyes
 
Ad

Similar to 2 BytesC++ course_2014_c3_ function basics&parameters and overloading (20)

PPTX
CP 04.pptx
RehmanRasheed3
 
PPTX
C++ process new
敬倫 林
 
PPT
Oop1
Vaibhav Bajaj
 
PPT
Cpp tutorial
Vikas Sharma
 
PDF
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
PPTX
Lecture 9_Classes.pptx
NelyJay
 
PDF
C++ practical
Rahul juneja
 
PPT
CppTutorial.ppt
HODZoology3
 
PPTX
Managing console
Shiva Saxena
 
DOCX
C++ file
Mukund Trivedi
 
DOCX
C++ file
Mukund Trivedi
 
PPT
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
edukuldeep2005
 
PPTX
Add an interactive command line to your C++ application
Daniele Pallastrelli
 
PPTX
Lec 2.pptx
AbdulMaalik19
 
PPTX
Chapter i(introduction to java)
Chhom Karath
 
PDF
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
PDF
Look Ma, “update DB to HTML5 using C++”, no hands! 
aleks-f
 
DOC
Pads lab manual final
AhalyaR
 
PPTX
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
CP 04.pptx
RehmanRasheed3
 
C++ process new
敬倫 林
 
Cpp tutorial
Vikas Sharma
 
OOP_EXPLAINED_example_of_cod_and_explainations.pdf
DerekDixmanChakowela
 
Lecture 9_Classes.pptx
NelyJay
 
C++ practical
Rahul juneja
 
CppTutorial.ppt
HODZoology3
 
Managing console
Shiva Saxena
 
C++ file
Mukund Trivedi
 
C++ file
Mukund Trivedi
 
3.pptirgggggggggggggggggggggggggggrrrrrrrrrrger
edukuldeep2005
 
Add an interactive command line to your C++ application
Daniele Pallastrelli
 
Lec 2.pptx
AbdulMaalik19
 
Chapter i(introduction to java)
Chhom Karath
 
54602399 c-examples-51-to-108-programe-ee01083101
premrings
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
Look Ma, “update DB to HTML5 using C++”, no hands! 
aleks-f
 
Pads lab manual final
AhalyaR
 
Oop object oriented programing topics
(•̮̮̃•̃) Prince Do Not Work
 
Ad

More from kinan keshkeh (20)

PDF
10 Little Tricks to Get Your Class’s Attention (and Hold It)
kinan keshkeh
 
PDF
Simpson and lagranje dalambair math methods
kinan keshkeh
 
PDF
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
PDF
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
PDF
GeneticAlgorithms_AND_CuttingWoodAlgorithm
kinan keshkeh
 
PDF
Algorithm in discovering and correcting words errors in a dictionary or any w...
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c8_units
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c7_double_lists
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c6_single linked list
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c5_pointers
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c4_binaryfiles
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c3_txtfiles
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c2_records
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c13_ templates
kinan keshkeh
 
PDF
2 BytesC++ course_2014_c12_ polymorphism
kinan keshkeh
 
10 Little Tricks to Get Your Class’s Attention (and Hold It)
kinan keshkeh
 
Simpson and lagranje dalambair math methods
kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
Shapes and calculate (area and contour) / C++ oop concept
kinan keshkeh
 
GeneticAlgorithms_AND_CuttingWoodAlgorithm
kinan keshkeh
 
Algorithm in discovering and correcting words errors in a dictionary or any w...
kinan keshkeh
 
2Bytesprog2 course_2014_c9_graph
kinan keshkeh
 
2Bytesprog2 course_2014_c8_units
kinan keshkeh
 
2Bytesprog2 course_2014_c7_double_lists
kinan keshkeh
 
2Bytesprog2 course_2014_c6_single linked list
kinan keshkeh
 
2Bytesprog2 course_2014_c5_pointers
kinan keshkeh
 
2Bytesprog2 course_2014_c4_binaryfiles
kinan keshkeh
 
2Bytesprog2 course_2014_c3_txtfiles
kinan keshkeh
 
2Bytesprog2 course_2014_c2_records
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2 BytesC++ course_2014_c13_ templates
kinan keshkeh
 
2 BytesC++ course_2014_c12_ polymorphism
kinan keshkeh
 

Recently uploaded (20)

PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PPTX
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Home Care Tools: Benefits, features and more
Third Rock Techkno
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
Transforming Mining & Engineering Operations with Odoo ERP | Streamline Proje...
SatishKumar2651
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 

2 BytesC++ course_2014_c3_ function basics&parameters and overloading

  • 1. Kinan keshkeh IT Engineering-Damascus University 3rd year Summer course- 2014 2 bytes team
  • 2. • Welcome again ! • Who has any Ques? • So Let’s Go ! • What we will take : • Functions. • parameters& overloading
  • 4. Functions •Two ways to write functions in your program: •Declaration / calling / definition
  • 5. Functions •Two ways to write functions in your program: •Declaration / calling / definition Before main Inside main After main
  • 6. Functions •Two ways to write functions in your program: •Declaration / calling / definition •Declaration + definition / calling
  • 7. Functions •Declaration / calling / definition #include<iostream> using namespace std ; Float avg(float x, float y ) ; int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avg : "<<c<<endl; system("pause"); return 0; } float avg(float x, float y ) { return (x+y)/2; }
  • 8. #include<iostream> using namespace std ; Float avg(float x, float y ) ; int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avg : "<<c<<endl; system("pause"); return 0; } float avg(float x, float y ) { return (x+y)/2; } Functions •Declaration / calling / definition Declaration
  • 9. #include<iostream> using namespace std ; Float avg(float x, float y ) ; int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avg : "<<c<<endl; system("pause"); return 0; } float avg(float x, float y ) { return (x+y)/2; } Functions •Declaration / calling / definition Declaration calling
  • 10. #include<iostream> using namespace std ; Float avg(float x, float y ) ; int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avg : "<<c<<endl; system("pause"); return 0; } float avg(float x, float y ) { return (x+y)/2; } Functions •Declaration / calling / definition Declaration calling Definition
  • 11. Functions •Declaration + definition / calling #include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; }
  • 12. Functions •Declaration + definition / calling #include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } Declaration+ definition
  • 13. Functions •Declaration + definition / calling #include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } Declaration+ definition calling
  • 14. Functions #include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } Returned value
  • 15. Functions #include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } It could be Void , then no return and no “ = ”
  • 16. Functions #include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } Attention !! “ , ” Not “ ; ”
  • 17. Functions #include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } Variables like in procedures & functions in Pascal !
  • 18. Functions #include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; } Also , it could be null Float avg()
  • 20. Predefined Functions •It’s existing in libraries in C++ •You can call it without writing any definition or declaration !! •You have to put using namespace std with all functions !
  • 21. Predefined Functions Name Description Type of Arguments Type of Value Example Value Library Header sqrt Square root double double sqrt(4.0) 2.0 cmath pow Powers double double pow(2.0,3.0) 8.0 cmath abs Absolute value for int int int abs(-7) abs(7) 7 7 cstdlib labs Absolute value for long long long labs(-70000) labs(70000) 70000 70000 cstdlib fabs Absolute value for double double double fabs(-7.5) fabs(7.5) 7.5 7.5 cmath ceil Ceiling (round up) double double ceil(3.2) ceil(3.9) 4.0 4.0 cmath floor Floor (round down) double double floor(3.2) floor(3.9) 3.0 3.0 cmath exit End program int void exit(1); None cstdlib rand Random number None int rand( ) Varies cstdlib srand Set seed for rand unsigned int void srand(42); None cstdlib
  • 22. Predefined Functions Name Description Type of Arguments Type of Value Example Value Library Header sqrt Square root double double sqrt(4.0) 2.0 cmath pow Powers double double pow(2.0,3.0) 8.0 cmath abs Absolute value for int int int abs (-7) abs(7) 7 7 cstdlib labs Absolute value for long long long labs(-70000) labs(70000) 70000 70000 cstdlib fabs Absolute value for double double double fabs(-7.5) fabs(7.5) 7.5 7.5 cmath ceil Ceiling (round up) double double ceil(3.2) ceil(3.9) 4.0 4.0 cmath floor Floor (round down) double double floor(3.2) floor(3.9) 3.0 3.0 cmath exit End program int void exit(1); None cstdlib rand Random number None int rand( ) Varies cstdlib srand Set seed for rand unsigned int void srand (42); None cstdlib
  • 23. ///Computes the size of a doghouse that can be purchased //given the user’s budget. #include <iostream> #include <cmath> using namespace std; int main( ){ const double COST_PER_SQ_FT = 10.50; //cost per square feet double budget, area, lengthSide; cout << "Enter the amount budgeted for your doghouse $"; cin >> budget; area = budget/COST_PER_SQ_FT; lengthSide = sqrt(area); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "For a price of $" << budget << endl << "I can build you a luxurious square doghousen" << "that is " << lengthSide << " feet on each side.n"; return 0; } Predefined Functions
  • 24. #include <iostream> #include <cstdlib> using namespace std; int main( ){ int month, day; cout << "Welcome to your friendly weather program.n" << "Enter today’s date as two integers for the month and the day:n"; cin >> month; cin >> day; srand(month*day); //using seed number=month*day int prediction; char ans; cout << "Weather for today:n"; do{ prediction = rand( ) % 3 ;// Scaling switch (prediction){ case 0: cout << "The day will be sunny!!n"; break; case 1: cout << "The day will be cloudy.n"; break; case 2: cout << "The day will be stormy!n"; break; default: cout << "Weather program is not functioning properly.n"; } cout << "Want the weather for the next day?(y/n): "; cin >> ans; } while (ans == 'y' || ans == 'Y'); cout << "That's it from your 24-hour weather program.n"; Return 0 ; } Code discussion 3
  • 25. Pseudorandom Number Generator Code discussion 3 The code does …
  • 26. Local & global Variables !
  • 27. Local & global Variables •Local Variables •It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . •You can use the same name of variables in different scopes ! #include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; } Block!
  • 28. Local & global Variables •Local Variables •It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . •You can use the same name of variables in different scopes ! #include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
  • 29. Local & global Variables •Local Variables •It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . •You can use the same name of variables in different scopes ! #include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
  • 30. Local & global Variables •Local Variables •It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . •You can use the same name of variables in different scopes ! #include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
  • 31. Local & global Variables •Local Variables •It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . •You can use the same name of variables in different scopes ! #include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
  • 32. Local & global Variables •Local Variables •It’s scope is the current ‘Block’ and all the ‘nested- blocks’ . •You can use the same name of variables in different scopes ! #include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
  • 34. Local & global Variables •Global Variables •At the first of program ( after “using namespace std” ) •It’s scope , all ‘blocks’ •If you have global variable (name x ) and other local variable ( name x too ) •If you want global x , you will write ::x
  • 37. Calling by value & by reference
  • 38. Calling by value & by reference
  • 39. Calling by value & by reference Without var With var
  • 40. Calling by value & by reference Without var With var But “ & ” instead of var .
  • 42. •Calling by value •The Output :
  • 45. •Calling by reference •The Output :
  • 47. Overloading •The same functions names ,but different meanings •Returned_value Fname ( int v1 , double v1 …. ) •Returned_value Fname ( double v1 , double v1 …. ) •Returned_value Fname ( char v1 , double v1 …. )
  • 48. Overloading •The same functions names ,but different meanings •Returned_value Fname ( int v1 , double v1 …. ) •Returned_value Fname ( double v1 , double v1 …. ) •Returned_value Fname ( char v1 , double v1 …. ) Difference here At parameters !
  • 49. Overloading •The same functions names ,but different meanings •Returned_value Fname ( int v1 , double v1 …. ) •Returned_value Fname ( double v1 , double v1 …. ) •Returned_value Fname ( char v1 , double v1 …. ) Difference here At parameters ! In numbers of them , and in their kinds
  • 50. Overloading •The same functions names ,but different meanings •Returned_value Fname ( int v1 , double v1 …. ) •Returned_value Fname ( double v1 , double v1 …. ) •Returned_value Fname ( char v1 , double v1 …. ) Difference here At parameters ! In numbers of them , and in their kinds Not here at returned value !
  • 51. Overloading •The same functions names ,but different meanings •Returned_value Fname ( int v1 , double v1 …. ) •Returned_value Fname ( double v1 , double v1 …. ) •Returned_value Fname ( char v1 , double v1 …. ) •You will see it on operators + , - , * ….
  • 57. Homework •Write a program that calls a function , in function the user input a Float number (ex: 3.6) , then the output is “ (3) and (0.6) “ •Write a program that calls a Recursive function , in function the user input an integer number (ex: 3) , then the output is “ (3! = 6) “
  • 58. That’s for today That’s for today guys !!
  • 59. That’s for today Go and Play !
  • 60. 2 bytes team Group : group link Mobile phone- Kinan : 0994385748 Facebook account : kinan’s account 2 bytes team