Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Structure of a Function in C:
- The portion of C code that contains the program of a function is the function definition.
- The following illustrates itβs form.
- /* You would put your block comment here describing what the function does */
- type function_name (parameters with types){
- Local declarations
- Local statements including the return statement.
- }
- Review of Functions and passing by value:
- Review the following code and understand the function, the flow of control and when
- it returns to the caller.
- /* This program demonstrates that one function can be
- called multiple times.
- Written by:
- Date:
- */
- #include <stdio.h>
- // Function Declarations
- void printOne (int x);
- int main (void)
- {
- // Local Declarations
- int a;
- // Statements
- a = 5;
- printOne (a); //first call
- a = 33;
- printOne (a); // Second call
- // Done. Return to operating system.
- return 0;
- } // main
- /* =================== printOne ===================
- Print one integer value.
- Pre x contains number to be printed
- Post value in x printed
- */
- void printOne (int x)
- {
- // Statements
- printf("%d\n", x);
- return;
- } // printOne
- Example Code for Pointers:
- This code will swap the values of A and B using a function and pointers.
- void exchange(int*, int*); //Function prototype
- int main(void){
- int a, b;
- exchange(&a,&b); //Function call
- return 0;
- }
- /* Function definition */
- void exchange(int* pa, int* pb)
- {
- int c;
- c = *pa;
- *pa = *pb;
- *pb = c;
- return;
- }
- Review of Pointers:
- Construct a program that prompts the user to enter in two values and store them into variables
- of type double. Declare two pointer variables of type double and assign them to point to
- the variables declared in the previous step. Now add the two inputs together using the
- POINTERS to the two variables and assign the result to a new value variable, print it to
- screen.
- /* SKELETON CODE */
- #include <stdio.h>
- int main()
- {
- double var1 = 0.0;
- double var2 = 0.0;
- double result = 0.0;
- //POINTERS GO HERE
- printf("Enter in two values:\n");
- scanf("%lf %lf", &var1, &var2);
- //MATH GOES HERE
- printf("%.2lf and %.2lf = %.2lf\n", var1, var2, result);
- return 0;
- }
- PRACTICE PROBLEMS
- Problem #1
- A. Write a program that prompts the user to enter in a value for the base and side of a
- square. Create a variable that will hold the area of the square. Compute the area using the
- pow() function in the math.h library. Print out the area of the square with a
- minimum width of 8 and precision of 2 decimal places to the right.
- (NO FUNCTIONS OR POINTERS USED IN PART A)
- B. Now compose a function to perform the calculation of the area of a square. The parameters
- will be the base of the square, and the return value will be the area. Now create the function
- prototype in the global area of the source code. Copy the function prototype and
- paste it under main, swap out the semicolon with an open curly brace
- and write your function definition.
- Test your function by running the program.
- BONUS: Modify your program from part B to use pointers. It should take only pointer parameters,
- and have no return values (Meaning it returns void). Remember, the function TYPE must change to void to return void.
- For example: "double calcArea(Your Parameters here){" returns a double, and you need to
- change it to "void calcArea(Your Parameters Here){" to return void (i.e. it doesn't have a value to return).
- Problem #2
- Write a program that generates a few random numbers using a call to the random number
- function and prints it to the screen. Run the program 2β3 times and observe the results.
- Now add the srand() call with the time() argument to add randomness to the numbers. Run
- the program a few more times and observe the results.
- Problem #3:
- Create a program that will generate a random set of numbers between 10 and 20 inclusively.
- Example:
- First, set the range: range = (max value - min value) + 1;
- Second, shift to the right: rand() % range + 1; //this starts the counting at 1 instead of zero.
- Examine the following expressions and determine the result. 1 or 0?
- 1.) 1 > 5
- 2.) x > 10 given int x = 10;
- 3.) (x > 10) || (y > 20) given int x = 7; int y = 20;
- 4.) (x > 10) && (y > 20) given int x = 7; int y = 20; Does the second expression even happen?
Add Comment
Please, Sign In to add comment