Lec 19- Functions-II

Lec 19- Functions-II - Lecture : Functions-II Course:...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
FAST, National University of Computer and Emerging Sciences, Islamabad Lecture : Functions-II Course: Programming For Engineers-1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sample function int add2ints(int a, int b) { return(a+b); } Return type Function name parameters Function body
Background image of page 2
Writing a function You have decide on what the function will look like: Return type Name Types of parameters (number of parameters) You have to write the body (the actual code).
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function parameters The parameters are local variables inside the body of the function. When the function is called they will have the values passed in . The function gets a copy of the values passed in (we will later see how to pass a reference to a variable).
Background image of page 4
Sample Function int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; // just to make a point firstnum = 0; secondnum = 0; return(sum); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Testing add2nums int main(void) { int y,a,b; cout << "Enter 2 numbers\n"; cin >> a >> b; y = add2nums(a,b); cout << "a is " << a << endl; cout << "b is " << b << endl; cout << "y is " << y << endl; return(0); }
Background image of page 6
What happens here? int add2nums(int a, int b) { a=a+b; return(a); } int a,b,y; y = add2nums(a,b);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Local variables Parameters and variables declared inside the definition of a function are local . They only exist inside the function body. Once the function returns, the variables no longer exist! That’s fine! We don’t need them anymore!
Background image of page 8
Block Variables You can also declare variables that exist only within the body of a compound statement (a block ) : { int foo; }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/21/2011 for the course EE 1 taught by Professor Shahidanwar during the Spring '10 term at NUCES.

Page1 / 37

Lec 19- Functions-II - Lecture : Functions-II Course:...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online