0% found this document useful (0 votes)
7 views6 pages

Chapter 3

The document discusses C++ functions including built-in functions, user-defined functions, passing arguments to functions, returning values from functions, and functions that check if a number is prime. It provides examples of using various types of functions like square, add_num, maximum, box_volume, functions with references, and a Prime function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Chapter 3

The document discusses C++ functions including built-in functions, user-defined functions, passing arguments to functions, returning values from functions, and functions that check if a number is prime. It provides examples of using various types of functions like square, add_num, maximum, box_volume, functions with references, and a Prime function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Chapter3

Built in function

#include <iostream>

#include<cmath>

using namespace std;

int main()

double n;

cin >> n;

cout << sqrt(n)<<endl;

return 0;

‫دوال يتم تعريفها من المستخدم‬

#include<iostream>

using namespace std;

int square(int);//function prototype

int main()

for(int x=1;x<=10;x++)

cout<<square(x)<<" ";

cout<<endl;

//now function definition

int square(int y)

return y*y;
}

Out example

#include <iostream>

using namespace std;

int add_num(int x,int y)

return x+y;

int main( )

cout << add_num(5,3) <<endl;

return 0;

#include <iostream>

using namespace std;

int add_num(int x,int y){

return x+y;

int main( )

int a,b,sum;

cin>> a>> b;

sum = add_num(a,b);

cout << sum <<endl;

return 0;

}
#include <iostream>

using namespace std;

int maximum (int, int, int);

int main( )

int a, b, c;

cout << "Enter three integers: " ;

cin >> a >> b >> c ;

cout << " maximum is : " << maximum (a, b, c) << endl;

return 0;

int maximum (int x, int y, int z)

int max = x;

if (y > x) max = y;

if (z > max) max = z;

return max;

void

#include <iostream>

using namespace std;

void f1 ( );

void f2 (void);

int main( )

f1 ( );
f2 ( );

return 0;

void f1 ( )

{ cout << "Function f1 takes no arguments" << endl; }

void f2 (void)

{ cout << "Function f2 also takes no arguments" << endl; }

‫استخدام نفس المتغيرات‬

#include <iostream>

using namespace std;

int maximum (int x, int y, int z)

int max = x;

if (y > x) max = y;

if (z > max) max = z;

return max;

int main( )

int x, y, z;

cout << "Enter three integers: " ;

cin >> x >> y >> z ;

cout << " maximum is : " << maximum (x, y, z) << endl;

return 0;

}
Default argument

using namespace std;

inline int box_volume (int length=1,int width=1,int height=1)

{return length*width*height;}

int main()

cout<<"The default box volume is "

<<box_volume() <<endl

<<"width 1 and height 1 is "

<<box_volume(10,2,2)<<endl;

return 0;

#include <iostream>

using namespace std;

void sum(int &x,int &y)

x=y;

y=0;

int main()

int x,y;

cin >> x>> y ;


cout<< x<< " and "<<y<<endl;

sum(x,y);

cout<< x<< " and "<<y<<endl;

return 0;

 ‫دالة ترجع هل العدد أولي أم ال‬

#include <iostream>

using namespace std;

int Prime(int x)

int pri= 1;

for(int i=2; i < x; i++)

if (x % i == 0) { pri= 0; break; }

return pri;

int main()

int a;

cin >>a;

cout <<Prime(a) <<endl;

return 0;

You might also like