Exercise 11 Functions
Eric Chen (last modified 2019.04.21)
Intended Learning Outcomes
When you have done this exercise, you will
1) Understand C function and C program structure
2) Be able to pass data to a function
3) Be able to get the return value from a function
4) Understand C scope rules (local variables vs global variables)
You need to download the example codes in folder 5. Functions for this exercise.
Task 1 max3
With this exercise, you are going to learn how to pass data to a function and how to get the return value. And how to compile a program contained in more than one file.
Given the following program (max3.c):
/* the prototype for function max2*/
int max2 ( int a, int b );
main ( ) {
int number1, number2, number3; int max;
/* read in an integer*/
printf(“Enter 3 integer numbers: “);
scanf(“%d %d %d”, & number1, &number2, &number3 );
/* find the maximum value of number1, number2 and number3*/ max = max2( number1, number2);
max = max2( max, number3);
/* print the max value*/
printf(“\nmax (%d, %d, %d) = %d\n”, number1, number2, number3, max); }
The max2( ) is given in file max2.c:
}
1) Learn how to create a C project, and add these 2 files to the project
2) Compile and run the program. Does your program give your right result?
int max2 ( int a, int b){
if (a > b )
return a;
else
return b;
1
Task 2 Scope Rules
With this exercise, you learn about the scope rules and data passing in C functions.
Read the following program ( confusion.c). What will be the output? Then run the program to check if the output is the same as your understanding. If you could not understand, it would better to use the debugging tool (learn how to use it by watching the video clip given in lecture 5-2) to track the values of the variables.
int confusion (int x, int y){ x = 2 * x + y;
return x;
}
int main (void) {
int x = 2, y = 5;
y = confusion (y, x);
x = confusion (y, x); printf (“%d %d\n”, x, y);
return 0; }
Task 3 Computation of Body Mass Index(BMI)
Write a function that compute the body mass index for given height and weight. Write also a test program to test your function. Read more about BMI at https://sv.wikipedia.org/wiki/Body_Mass_Index.
If you want that this program is useful, you can output if a person is underweight, normal weight, overweight, or obese.
Task 4 Star Pyramid
The complete example on program development process in C was given in the folder 2. Computer Algorithms. Read the implementation and re-implement solutions to the sub-problems “get valid size” and “print the pyramid” in 2 functions.
Task 5 Siff_sum (optional)
(in Swedish) Skriv en function siff_sum som får ett heltal av typen unsigned int som parameter. Funktionen skall summera sifforna i talet och get denna summa som returnvärde. Tips. Dividera upprepade gånger med 10 och summerar resterna vid divisionerna.
Write a test program to read an unsigned int, call the function siff_sum to compute the sum of digits, and prints the result.
Example: if the number is 1234, the siff_sum will return 10 ( = 4 + 3 + 2 + 1).
2