Lec 19- Functions-II

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

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

FAST, National University of Computer and Emerging Sciences, Islamabad Lecture : Functions-II Course: Programming For Engineers-1

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

View Full Document
Sample function int add2ints(int a, int b) { return(a+b); } Return type Function name parameters Function body
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).

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

View Full Document
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).
Sample Function int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; // just to make a point firstnum = 0; secondnum = 0; return(sum); }

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

View Full Document
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); }
What happens here? int add2nums(int a, int b) { a=a+b; return(a); } int a,b,y; y = add2nums(a,b);

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

View Full Document
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!
Block Variables You can also declare variables that exist only within the body of a compound statement (a block ) : { int foo; }

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

View Full Document
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
Ask a homework question - tutors are online