chapter 6 - function (part 2)_2014

26
CSC099: COMPUTING ENGINEERING II Chapter 6 : Function and Prograe Structure !Part "# 1

Upload: munext96

Post on 04-Oct-2015

223 views

Category:

Documents


0 download

DESCRIPTION

program

TRANSCRIPT

Chapter 7 Array

CSC099: COMPUTING ENGINEERING IIChapter 6 : Function and Programme Structure(Part 2)1ObjectiveTo understand the difference between pass by value and pass by reference2Passing variables to a Function In memory location, a variable has both a value and a unique addressIn passing a variable to function, you can pass either the variables value (pass by value) or its address (pass by reference) to the receiving function3Passing by valueIn a pass by value, the computer passes the contents of the variable to the receiving functionReceiving function is not given access to the variable in memoryIt cannot change value stored inside variableBy default, variables are passed by value in C

4Example : Pass by value5/*prototype declaration */void passByValue (int x);

int main (void){

/*local definition */ int a = 5;

/*statement*/ passByValue (a); printf(%d\n, a); return 0;}void passByValue (int x){ /*statement*/ x = x + 3; return;}axvalues copiedprints 56Example Pass by value6#include

void test(int a);

int main ( ){ int x=50; test(x); printf(Value x now is %d, x);}

void test(int a){ a += 50;}

Example Pass by value7#include

void passByValue(int k);

int main ( ){ int i=0; printf (" The value of i before call %d \n", i); passByValue (i); printf (" The value of i after call %d \n", i);}

void passByValue(int k){ k = k + 10;}The value of i before call 0 The value of i after call 0Example Pass by value88#include

void changeMe(int num);

int main ( ){ int num = 12; printf (" In main function, number is %d, num); changeMe (num); printf (" Back in main again, number now is %d, num);}

void changeMe(int num){ num = 0; printf(In changeMe, the value is %d, num);}

Pass by referencePassing a variables address is referred to as passing by referenceReceiving function has access to passed variableUse ONLY when you want receiving function to change the contents of variableTo pass a variable by reference in CInclude dereference operator (*) after the type of formal parameter in function prototype and function headerInclude an address-of operator(&) before the variable of actual parameter in function call9Example 1: Pass by reference10/*prototype declaration */void passByRef(int *x);

int main (void){ /*local definition */ int a = 5;

/*statement*/ passByRef (&a); printf(%d\n, a); return 0;} void passByRef (int *x){ /*statement*/*x = *x + 3;return;}

aPrints 8xDereferenceAddress (pointer)After the type of parameter, include dereference operator (*)Use address operator (&) before the variable to be passedRequire dereference operator (*)1111Example Pass by REFERENCE11#include

void test(int *a);

int main ( ){ int x=50; test(&x); printf(Value x now is %d, x);}

void test(int *a){ *a += 50;}

Example 2: Pass by reference12#includevoid passByRef(int*k) ;

int main(){inti=0;

printf("Thevalueofibeforecall%d\n",i);passByRef(&i);printf("Thevalueofiaftercall%d\n",i);}void passByRef(int*k){*k=*k+10;}The value of i before call 0 The value of i after call 10Example Pass by REFERENCE131313#include

void changeMe(int *aValue);

int main ( ){ int num = 12; printf (" In main function, number is %d, num); changeMe (&num); printf (\nBack in main again, number now is %d, num);}

void changeMe(int *myValue){ *myValue = 0; printf(\nIn changeMe, the value is %d, *myValue);}

Pass by value vs. Pass by reference Pass by valuea copy of data is created and placed in a local variable in the called functionensure that regardless of how the data is manipulated and changed in the called function, the original data in the calling function are safe and unchangedPass by referencesends the address of a variable to the called functionuse the address operator (&) in the parameter of the called functionanytime we refer to the parameter, therefore we actually referring to the original variableif the data is manipulated and changed in the called function, the original data in the calling function are changed14Pass by value vs. Pass by reference15#include

void passByValue(int, int);void passByReference(int *, int);

int main(){ int a=3; passByValue(a,4); printf("\nThe value of a after passByValue() is %d", a);

a=3; passByReference(&a,4); printf("\nThe value of a after passByReference() >> %d\n", a);}void passByValue(int x, int y){ int t=6; x +=t; t+=2*y;}void passByReference(int *x, int y){ int t=6; *x +=t; t+=2*y;}

Exercise 1 16int main(){int num = 1;printf(In main, num is %d\n, num);anotherFunction();printf(Back in main, num is %d\n, num);return 0;}void anotherFunction(){int num = 20;printf(In anotherFunction, num is %d\n, num);}

Exercise 217void twin(int *);int main(){int value = 4;printf("In main, value is %d\n", value);printf("Now calling twin...\n");twin(&value);printf("Now back in main, the value is %d\n", value);return 0;}void twin(int *refVal){if (*refVal >= 2)*refVal *= 2;else*refVal -= 2;}

Exercise18int a;void f(int x, int y) { x += a; a += 3*y;}

int main() { int a = 9; f(a, 4); printf("%d\n", a);return 0;}

int a;void f(int *x, int y) { *x += a; a += 3*y;}

int main() { int a = 9; f(&a, 4); printf("%d\n", a); return 0;}

Exercise 4 Trace output19void get_numbers(int *input1, int *input2);void swap_values(int *var1, int *var2);int main(){ int first=7, second=5; get_numbers(&first, &second); swap_values(&first, &second); printf("The reverse order of the two numbers is %d %d", first, second);return 0;}void get_numbers(int *input1, int *input2){printf("Enter two integers: "); scanf("%d %d", &input1,&input2);}void swap_values(int *var1, int *var2){int temp;temp = var1;var1 = var2;var2 = temp;}

Exercise 5 Modify codeModify previous code so that the program will display the following output.

Enter two integers: 1 2The reverse order of the two numbers is 2 120

Define the variable sumptr to be a pointer to an object of type float.Assign the address of variable total to pointer sumptr.Assign the value of pointer sumptr to variable total2. print the value of total2.Write the function header for a function called money that takes two pointers of floating-point numbers p and q as parameters and does not return a value.

21ExerciseExercise 222void anotherFunction(int *daisy, int rose);int main(void){int first, second;first = 5;second = 10;anotherFunction(&second, first);printf("%4d%4d\n", first, second);return;}void anotherFunction(int *daisy, int rose){int flower;

flower = 25;rose = 5 + flower;*daisy = flower * rose;}

exercise23#include void do_something(int *thisp, int that);int main(void){int first, second;first = 1;second = 2;do_something(&second, first);printf("%4d%4d\n", first, second);return (0);}void do_something(int *thisp, int that);{int the_other = 5;that = 2 + the_other;*thisp = the_other * that;}ANSWER??

A. 35 2B. 1 35C. 35 7D. 1 2

Exercise24void num(int *z);int x, y;int main(){x = 2; y = 3;printf("%d\t%d\n", x, y);num(&x);printf("%d\t%d\n", x, y);num(&y);printf("%d\t%d\n", x, y);return(0);}

void num(int *z){int y;y = *z + 2;*z = y * 3;}

Exercise 325#include void funOne (int a, int *b, char c);int main(){int num1 = 10, num2 = 15;char ch = 'A';printf("Line 1: Inside main: num1 = %d, num2 = %d, and ch = %c\n", num1, num2, ch);funOne(num1, &num2, ch);printf("Line 3: After funOne: num1 = %d, num2 = %d, and ch = %c\n", num1, num2, ch);return 0;}void funOne(int a, int *b, char c){ int one; one = a; a++; b = *b * 2; c = 'B';printf("Line 2: Inside funOne: a = %d, b = %d, c = %c, and one = %d\n", a, b, c, one);}

26#include void funOne (int a, int *b, char c);int main(){int num1 = 10, num2 = 15;char ch = 'A';printf("Line 1: Inside main: num1 = %d, num2 = %d, and ch = %c\n", num1, num2, ch);funOne(num1, &num2, ch);printf("Line 3: After funOne: num1 = %d, num2 = %d, and ch = %c\n", num1, num2, ch);return 0;}void funOne(int a, int *b, char c){int one;one = a;a++;b = *b * 2;c = 'B';printf("Line 2: Inside funOne: a = %d, b = %d, c = %c, and one = %d\n", a, b, c, one);}