Functions Part 2-Introduction to Computer Programming-Lecture Slides, Slides of Computer Programming

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

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal 🇮🇳

3

(1)

30 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Handout on Functions
Passing Values to Functions
#include <iostream.h>
int mult(int, int);
int main()
{int a = 10, b = 20;
cout << mult(a, b);
//cout << x << y;
// *** Error *** --unknown identifiers x, y
return 0;
}
int mult(int x, int y) // can have different names here
{return x*y;
}
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Functions Part 2-Introduction to Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Handout on Functions

Passing Values to Functions #include <iostream.h>int mult(int, int);int main(){ int a = 10, b = 20;cout << mult(a, b);//cout << x << y;// *** Error *** --unknown identifiers x, yreturn 0;} int mult(int x, int y) //

can have different names here

{ return x*y;}

Handout on Functions

Passing Pointers to Functions #include <iostream.h>void f(int *j);//or void f(int *);int main(){ int i;int *p;p = &i; // p now points to if(p);cout << i;

// i is now 100

return 0;} void f(int *j){ *j = 100; // var pointed to by j is assigned 100}

Handout on Functions

Passing Arrays to 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++;} }

  • Original

contents:

  • Altered

contents:

Handout on Functions

ReturningPointers

#include <iostream.h>char *get_substr(char *sub, char *str);int main(){ char *substr;substr = get_substr("three", "one two three four");cout << "substring found: " << substr;return 0;}

char *get_substr(char *sub, char *str){ int t;char *p, *p2, *start;for(t=0; str[t]; t++){

p = &str[t]; // reset pointersstart = p;p2 = sub;while(*p2 && p2==p){ //check for substring

p++;p2++;

} /* If at end of p2 (i.e., substring),then a match has been found. /if(!p2)return start;/* return pointer to beginning ofsubstring */} return 0; // no match found

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

Passing Reference to Functions #include

<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

}