Laboratory Work №2 Theme: Implementation of functions, parameters and data transfer Aim



Download 263,25 Kb.
bet1/4
Sana01.01.2022
Hajmi263,25 Kb.
#293403
  1   2   3   4
Bog'liq
DSA Lab2


Laboratory Work 2

Theme: Implementation of functions, parameters and data transfer

Aim: Developing skills in programming semi-static data structures.

In general, we can hide the details of any computation by defining a function. A function definition requires a name, a group of parameters, a return type, and a body. It may either return a variable, value, or nothing (specified by the keyword void). For example, the simple function defined below returns an integer which is the double of the value you pass into it.

#include

using namespace std;


int timesTwo(int num) {

/* return type int which indicates

that an integer is being returned */

return num*2;

}
int main() {

/* return type int which indicates that

an integer is being returned */

cout<
return 0;

}


The syntax for this function definition includes the name, timesTwo, and a parenthesized list of formal parameters and their types. For this function an int named num is the only formal parameter, which suggests that timesTwo needs only one piece of data to do its work. The details, hidden “inside the box,” simply compute the result of num*2 and return it. We can invoke or call the timesTwo function by asking the C++ to evaluate it, passing an actual parameter value, in this case, 3. Note that the call to timesTwo returns an integer that can in turn be passed to another invocation.

Let us look at a similar function.



Implementation of the timesTwoVoid function:

#include

using namespace std;
void timesTwoVoid(int num) {

/* return type void which indicates

that an nothing is being returned */

cout<< num*2<

}
int main() {

/* return type int which indicates that

an integer is being returned */

timesTwoVoid(5);


return 0;

}

Take a look at the code below:



#include

using namespace std;


void dogWalk(int steps){

Download 263,25 Kb.

Do'stlaringiz bilan baham:
  1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish