2 bytesc++ course_2014_c3_ function basics&parameters and overloading

60
Kinan keshkeh IT Engineering-Damascus University 3 rd year Summer course- 2014 2 bytes team

Upload: kinan-ke

Post on 04-Jul-2015

29 views

Category:

Software


1 download

DESCRIPTION

This is my C++ course / basic , intermediate and a little advance

TRANSCRIPT

Page 1: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Summer course- 2014

2 bytes team

Page 2: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Welcome again !

• Who has any Ques?

• So Let’s Go !

• What we will take :

• Functions. • parameters& overloading

Page 3: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Function Basics!

Page 4: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Functions

• Two ways to write functions in your program:

• Declaration / calling / definition

Page 5: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Functions

• Two ways to write functions in your program:

• Declaration / calling / definition

Before main Inside main After main

Page 6: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Functions

• Two ways to write functions in your program:

• Declaration / calling / definition

• Declaration + definition / calling

Page 7: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 8: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

#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

Page 9: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

#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

Page 10: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

#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

Page 11: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 12: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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

Page 13: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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

Page 14: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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

Page 15: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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 “ = ”

Page 16: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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 “ ; ”

Page 17: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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 !

Page 18: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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()

Page 19: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Predefined Functions !

Page 20: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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 !

Page 21: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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

Page 22: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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

Page 23: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

///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 doghouse\n" << "that is " << lengthSide << " feet on each side.\n"; return 0; }

Predefined Functions

Page 24: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

#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

Page 25: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Pseudorandom Number Generator

Code discussion 3

The code does …

Page 26: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Local & global Variables !

Page 27: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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!

Page 28: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 29: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 30: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 31: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 32: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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; }

Page 33: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Local Variables

Page 34: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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

Page 35: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Global Variables

Page 36: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Parameters and Overloading

Page 37: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Calling

by value & by reference

Page 38: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Calling

by value & by reference

Page 39: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Calling

by value & by reference

Without var With var

Page 40: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Calling

by value & by reference

Without var With var

But “ & ” instead of var .

Page 41: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Calling by value

Page 42: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Calling by value

• The Output :

Page 43: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Calling by reference

Page 44: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Calling by reference

Page 45: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Calling by reference

• The Output :

Page 46: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Overloading !

Page 47: 2 BytesC++ course_2014_c3_ function basics&parameters and 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 …. )

Page 48: 2 BytesC++ course_2014_c3_ function basics&parameters and 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 …. )

Difference here At parameters !

Page 49: 2 BytesC++ course_2014_c3_ function basics&parameters and 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 …. )

Difference here At parameters !

In numbers of them , and in their kinds

Page 50: 2 BytesC++ course_2014_c3_ function basics&parameters and 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 …. )

Difference here At parameters !

In numbers of them , and in their kinds

Not here at returned value !

Page 51: 2 BytesC++ course_2014_c3_ function basics&parameters and 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 …. )

• You will see it on operators + , - , * ….

Page 52: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading
Page 53: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Overloading

Page 54: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Overloading

Page 55: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

Overloading

Page 56: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

• Default Arguments

Page 57: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

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) “

Page 58: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

That’s for today

That’s for today guys !!

Page 59: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

That’s for today

Go and Play !

Page 60: 2 BytesC++ course_2014_c3_ function basics&parameters and overloading

2 bytes team

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team