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

Functions

Uploaded by

ghaithelqetrany
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views6 pages

Functions

Uploaded by

ghaithelqetrany
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Functions ‫الدوال‬

‫تعريف الدالة‬
main() main()
‫الفائدة من استخدام الدوال‬
o
o
o

‫الصيغة العامة للدالة‬


data type function name (par1,par2,….)
{
local variables declaration ;
function body;
return(expression);
}

data type 1
: function name 2
(par1,par2… 3
local variables declaration 4
function body 5
expression return 6
‫برمجة تراكيب البيانات‬
‫استخدام الدالة‬

Function Declaration 1
main()


void 
:‫أمثله على بعض اإلعالنات للدوال‬
1-void display();
2-int example();
3- int mul (int ,int);
4- float div (int,int,int);
5-float average( int[],int);
6-void square_number(int);
Function Details 2
main()

int addition (int,int);

int addition (int a,int b)


{
int r;
r=a+b;
return r;
}
‫برمجة تراكيب البيانات‬

Function Calling 3
main()

1. addition(5,7);
2. addition (x,y);
3. Z= addition(x,y); z
:‫أمثله توضح استخدام الدوال في البرنامج‬
Welcome to C++ language 1
#include<iostream.h>
void display();
int main()
{
display();
return 0;
}
void display()
{
cout<<"welcome to C++ language \n";
}
2
#include<iostream.h>
int cube(int n)
{
return(n*n*n);
}
int main()
{
int x;
cout<<"enter x \n";
cin>>x;
cout<<"x^3="<<cube(x)<<endl;
return 0;
}
‫برمجة تراكيب البيانات‬

main() 

n 3

#include<iostream.h>
float average (int);
int main()
{
int n;
float avg;
cout<<"enter size of the numbers :\n";
cin>>n;
avg= average(n);
cout<<"the average of all numbers ="<<avg<<endl;
return 0;
}
float average(int a )
{
int i, value;

float sum=0;
cout<<"enter "<<a<<"values please=:"<<endl;
for(i=1;i<=a;i++)
{
cout<<"enter value "<<i<<"\n";
cin>>value;
sum+=value;
}
return sum/a;
}
Recursion Functions ‫الدوال ذاتية االستدعاء‬

overflow
‫برمجة تراكيب البيانات‬
1

n! n(n  1)( n  2)( n  3).........3.2.1


5.4.3.2.1 5
4.3.2.1 4
4 5 5!=5.4! 5

1 1 0 n!=n.(n-1)!

#include<iostream.h>
int f(int n)
{
if (n==0 || n==1)
return 1;
else
return n*f(n-1);
}
int main( )
{
int n;
cout<<"enter n:\n";
cin>>n;
cout<<"factorial "<<n<<"is "<<f(n)<<endl;
return 0;
}

:‫تدريب‬
1
sum=x2+x4+x6+…+xn
x
‫برمجة تراكيب البيانات‬
2

#include<iostream.h>
int fun( );
int main( )
{
int i;
i=9;
while(i<=14)
{
cout<<fun()<<"\n";
++i;
}
return 0;
}
int fun( )
{
int i=6;
int j=10;
return(i++ + ++j);
}

sum 3
2 3 4 n 1
sum     ........ 
3 5 7 2n  1

You might also like