This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Data Structure And Algorithms
1-1 : esckþIepþIm ³emeronenHENnaMeGayEsVgyl;GMBIbBaða Data Structure and
Return type name function (list parameter ………){ Local variable statement; …………..}
16
ក��ងយោន្ទា7 return type រ មួម៉ានុំ ២ប្រប់យោmទ គឺ� void នុំ�ង data type
* Name function ជាយោ[� 7 function
* ប់G, parameter យោប្រប់�យោZយោព័ល់ Declaration Function យោuថា parameter ។
* ប់G, parameter ដែ*ល់ផ្ទុ,ល់ យោ<យ Function (ក��ង main program) យោZយោព័ល់យោយ�ងយោu Function យោuថា
Argument ។
* List Parameter រ មួម៉ានុំ ២ប្រប់យោmទគឺ� :
- Value parameter គឺ�ជា parameter ដែ*ល់មួ�នុំដែប្រប់ប្រប់Vល់ត�នៃល់មួ�នុំយោព័ល់ Call function នុំ�ងយោប្រ+យយោព័ល់ Call
function ។
- Reference parameter គឺ�ជា parameter ដែ*ល់ដែប្រប់ប្រប់Vល់ត�នៃល់មួ�នុំយោព័ល់ Call function នុំ�ងយោប្រ+យយោព័ល់ Call
function ។Example 1 : // passing parameters by reference
#include <stdio.h>
#include <conio.h>
void duplicate(int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
getch();
}
int main()
{ 17
18
int x=1,y=3,z=7;
duplicate(x,y,z);
printf("x=%d y=%d z=%d",x,y,z);
return 0;
}
Result : x=2, y=6, z=14
Example 2 : // more than one returning value
#include <stdio.h>
#include <conio.h>
void prevnext(int x, int& prev, int& next)
{
prev=x-1;
next=x+1;
getch();
}
int main()
{
int x=100,y,z;
prevnext(x,y,z);
printf("x=%d, Previous=%d, Next=%d",x,y,z);
return 0;
}
Result : X=100, Previous=99, Next=101
* រយោប់2ប់សំរយោសំរអ្ននុំ�គឺមួនុំC ម៉ានុំ ៤ដែប់ប់ដែ*ល់យោយ�ងប្រត=វបានុំយោល់�កយោD�ងគឺ� :ឧទាហរណC : គឺណន្ទាផ្ទុល់ប់�ក ២ច�នុំនុំគឺត a នុំ�ង b
១- អ្ននុំ�គឺមួនុំCម៉ានុំ data type and ម៉ានុំ parameter
int sum1( int a, int b )
{
int s= a + b ;
return (s);
}
ឧទាហរណC : Value parameter
void change (int x , int y); /* x and y ជា value parameter */
ឧទាហរណC : Reference parameter
void change (int& x , int& y); /* x and y ជា reference parameter */
២- អ្ននុំ�គឺមួនុំCម៉ានុំ data type and 5� នុំ parameter
int sum2()
{
int a,b;
printf("input a , b : ");
scanf("%d %d",&a,&b);
int s=a+b;
return(s);
}
19
20
៣- អ្ននុំ�គឺមួនុំC5� នុំ data type and ម៉ានុំ parameter
void sum3( int a , int b)
{
int s=a+b;
printf(“\n sum = %d “,s);
}
៤- អ្ននុំ�គឺមួនុំC5� នុំ data type and 5� នុំ parameter
void sum4()
{
int a,b;
printf("input a,b :");
scanf("%d %d ",&a,&b);
int s=a+b;
printf("\n sum=%d",s);
}
Storage Class
A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program.
There are following storage classes which can be used in a C Program
auto
register
static
Extern
21
auto - Storage Class
auto is the default storage class for all local variables.
{
int Count;
auto int Month;
}
The example above defines two variables with the same storage class. auto can only be used within functions, i.e.
local variables.
register - Storage Class
register is used to define local variables that should be stored in a register instead of RAM. This means that the
variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator
applied to it (as it does not have a memory location).
{
register int Miles;
}
Register should only be used for variables that require quick access - such as counters. It should also be noted that
defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a
register - depending on hardware and implimentation restrictions.
static - Storage Class
static is the default storage class for global variables. The two variables below (count and road) both have a static
22
storage class.
static int Count;
int Road;
{
printf("%d\n", Road);
}
extern - Storage Class
extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern'
the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously
defined.
When you have multiple files and you define a global variable or function which will be used in other files also, then
extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.