SFTW120-LectureNotes-07

SFTW120-LectureNotes-07 - SFTW120 Programming Science...

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

1 SFTW120 Programming Science Lecture Notes 7: C: Advanced Use of Functions (Textbook Chapter 6) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 7 Functions s A function is a program module that has a specific purpose s Simple functions: b Functions without input arguments/return value b Functions with input arguments b Functions with a return value b Functions with input arguments and a return value s More advanced functions: b Functions with output arguments s Functions with output arguments can return more than one value

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

View Full Document
2 SFTW120 - Programming Science 3 Lecture Notes 7 Simple functions without output parameters s Example: separating a real number into three parts (whole number, fractional part, sign) b Example: the parts of the number 12.345 are: b Whole number: 12 b Fractional part: 0.345 b Sign: + s Simple solution: write three functions 1. A function to get the whole number 2. A function to get the fractional part 3. A function to get the sign SFTW120 - Programming Science 4 Lecture Notes 7 Simple functions without output parameters /* Get the whole part of a real number */ int whole(double num) { int whole_num; /* floor function rounds down, fabs function gets absolute value */ whole_num = floor(fabs(num)); return(whole_num); }
3 SFTW120 - Programming Science 5 Lecture Notes 7 Simple functions without output parameters /* Get the fractional part of a real number */ double fract(double num) { double fract_num; /* fabs function gets absolute value */ fract_num = fabs(num) - whole(num); return(fract_num); } SFTW120 - Programming Science 6 Lecture Notes 7 Simple functions without output parameters /* Get the sign of a real number */ char sign(double num) { char sign_num; if (num < 0.0) sign_num = '-'; else if (num == 0.0) sign_num = ' '; else sign_num = '+'; return(sign_num); }

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

View Full Document
4 SFTW120 - Programming Science 7 Lecture Notes 7 Main function calling simple functions int main(void) { double value; /* input - number to analyze */ char sn; /* output - sign */ int whl; /* output - whole number */ double fr; /* output - fractional part */ /* Get data */ printf("Enter a value to analyze: "); scanf("%lf", &value); SFTW120 - Programming Science 8 Lecture Notes 7 Main function calling simple functions /* Separates data value into three parts */ sn = sign(value); whl = whole(value); fr = fract(value); /* Prints results */ printf("Parts of %.4f\n sign: %c\n", value, sn); printf(" whole number magnitude: %d\n", whl); printf(" fractional part: %.4f\n", fr); return(0); }
5 SFTW120 - Programming Science 9 Lecture Notes 7 Example run Example run: Enter a value to analyze: 12.345 Parts of 12.3450 sign: + whole number magnitude: 12 fractional part: 0.3450 SFTW120 - Programming Science 10 Lecture Notes 7 Analyzing calls to simple functions What happens in a function call to a simple function? Example:

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.

{[ snackBarMessage ]}

Page1 / 22

SFTW120-LectureNotes-07 - SFTW120 Programming Science...

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

View Full Document
Ask a homework question - tutors are online