Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REFERENCE MATERIAL
- 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.
- }
- 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;
- }
- FLOW OF CONTROL/FUNCTIONS EXAMPLE PROBLEM
- Answer the questions in the comments of this code BEFORE running it.
- Run it afterward to check your results!
- #include <stdio.h>
- //What are these two below statements called?
- int myFunction1(void);
- int myFunction2(int);
- // List the chronological order the functions execute in this program (Including main)
- int main(void){
- int x = 7;
- int y = 0;
- myFunction1(); //What does this do? What arguments does this take?
- myFunction2(x); //What does this do? What arguments does this take?
- y = myFunction1() + myFunction2(x); //What does y equal?
- //What is different between the printf function and myFunction1/myFunction2?
- printf("%d\n", y);
- return 0;
- }
- int myFunction1(void){
- return 5;
- }
- int myFunction2(int myVar){
- return myVar + 2;
- }
- Answer the questions in the comments of this code.
- Run it afterward to check your results, try changing the statements/values and see what you get!
- #include <stdio.h> /* for printf() */
- int main(void)
- {
- int i;
- int* p = &i; //What value does variable *p store?
- printf("%p\n", p); //What will this print? (Remember what the %p formatter does)
- return 0;
- }
- Problem #1 Using pointers
- Construct a program that prompts the user to enter in two type double values and
- store them into two type double variables. Then declare two pointer variables 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.
- Problem #2 Pass by Reference
- Using the program from problem 1, create a function called “AddNbrs()” that takes
- three double pointer variables and returns nothing to the caller. Keep the two input
- variables and the user prompts and outputs within main. Move the expression that adds the
- values into the new function. Then assign the result using the
- pointers passed to the function from main.
- /*** SKELETON CODE ***/
- #include <stdio.h>
- void AddNbrs(double*, double*, double*); //Function prototype given
- int main()
- {
- //START PROGRAMMING HERE
- return 0;
- }
- //WRITE FUNCTION HERE
- Problem #3
- Create a program that makes use of pointers to take in two user-supplied values, being the
- length of 2 sides of a 4 sided polygon (square or rectangle). Your program should
- prompt the user from inside main to enter in the length of two adjacent sides of a 2-
- sided polygon. It should then call a single function to compute the area AND perimeter of the
- polygon and return those values using pointers to the calling function. The output
- should be presented in a meaningful way to the user.
- Problem #4
- Create a program that prompts the user to enter in a value for number of
- seconds using a long data type. Create a function that when given a value
- representing seconds will return the number of hours, minutes and seconds
- contained in that value of seconds. While the function can take the seconds variable
- as input, the three measures should be returned to the calling
- function by using pointers.
- You may assume that the user will not enter more than 24 hours worth of seconds.
- Problem #5:
- Write a program that uses a function that receives a pointer to a type double
- number and two integer variables and sends back the integer and fraction parts as
- integers using pointers up to the hundredth place. In your main
- function prompt for the user to input a type double number, then call the
- function, then output the parts from main after the function returns them using
- pointers. Hint use modulus and division.
- So if a user enters in 98.65 , the function should return 98 and 65
Add Comment
Please, Sign In to add comment