tailieunhanh - Lecture Introduction to Computer Programming - Lecture 12

After studying this chapter you will be able to understand: Example program – nested loop, break and continue statement, case control structure, a menu driven program using if else, switch case, nested case control structure. | CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12 Writing a function You need to decide that how the function will look like: Return type Name Types of parameters (number of parameters) You have to write the body (the actual code). 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); } 4 Available via the course home page in code/functions/ Testing add2nums int main(void) { int y,a,b; cout > a >> b; y = add2nums(a,b); cout > 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?

TỪ KHÓA LIÊN QUAN