2 bytesc++ course_2014_c3_ function basics&parameters and overloading

Post on 04-Jul-2015

29 Views

Category:

Software

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

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 doghouse\n" << "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 + , - , * ….

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

top related