






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: Passing, Values, Functions, Pointers, Arrays, Strings, Returning, Pointers, Command, Line, Arguments
Typology: Slides
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Handout on Functions
Handout on Functions
Handout on Functions
#include <iostream.h>void display(int num[10]); //or display(int [10]);int main(){ int t[10],i;for(i=0; i<10; ++i) t[i]=i;display(t); // pass array t to a functioncout <<endl;for(i=0; i<10; i++) cout << t[i] << ' '; //output?} // Print some numbers.void display(int
num[10])
{ int i;for(i=0; i<10; i++) cout << num[i] << ' '; //output?for(i=0; i<10; i++) (num[i] = num[i] + 1);}
Handout on Functions
Passing Arraysto Functions
#include
<iostream.h> void^
cube(int
*n,^
int^ num);
int^ main(){ int
i,^ nums[10];for(i=0;
i<10;
i++)
nums[i]
=^ i+1;
cout^
<<^ "Original
contents:
for(i=0;
i<10;
i++)
cout
<<^ nums[i]
cout^
<<^ '\n'; cube(nums,
//^ compute
cubes
cout^
<<^ "Altered
contents:
for(i=0;
i<10;
i++)
cout
<<^ nums[i]
return
} void^
cube(int
*n,^
int^ num)
{ while(num){
*n^ =^
*n^ *^
*n^ *^
*n;
num--;n++;} }
contents:
contents:
Handout on Functions
ReturningPointers
Handout on Functions
CommandLineArguments #include
<iostream.h> int^ main(int
argc,
char
*argv[])
{ if(argc!=2)
cout^
<<^ "You
forgot
to^ type
your
name.\n";
return
} cout^
<<^ "Hello
argv[1]
<<^ '\n';
return
#include <iostream.h>int main(int argc, char *argv[]){ int t, i;for(t=0; t<argc; ++t){// t denotes the t
th^ string
i = 0;while(argv[t][i]){// t[i] accesses the i
th^ character of t
cout << argv[t][i];++i;
cout << ' '; }
cout << ' '; } return 0;}
Handout on Functions
<iostream.h> void
sqr_it(int
&x);
int^
main() { int
t^ =
10; cout
<<^
"Old
value
for
t:^
"^ <<
t^ <<
'\n';
sqr_it(t);
//^
pass
address
of^
t^ to
sqr_it()
cout
<<^
"New
value
for
t:^
"^ <<
t^ <<
'\n';
return
0; } void
sqr_it(int
&x)
{^ x
*=^
x;^ //
this
modifies
calling
argument
t
}
Handout on Functions
Returning References from Functions #include
<iostream.h> double
&f(); double
val
=^ 100.0; int^
main() { double
newval; cout
<<^
f()^
<<^ '\n';
//^
display
val's
value
newval
=^ f();
//^
assign
value
of^
val^
to^ newval
cout
<<^
newval
<<^
'\n';
//^
display
newval's
value
f()^
=^ 99.1;
//^
change
val's
value
cout
<<^
f()^
<<^ '\n';
//^
display
val's
new
value
return
0; } double
&f() {^ return
val;
//^
return
reference
to^
val
}