1 / 29

Functions in C

Functions in C. ATS 315. What are functions?. In math, you see functions all the time: x = sin(y) x = 3y 2. What are functions?. Functions are operations. They have INPUT and OUTPUT. x = sin(y). input. output. function. Functions in C. Some “functions” in C are built-in:

moya
Télécharger la présentation

Functions in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions in C ATS 315

  2. What are functions? • In math, you see functions all the time: x = sin(y) x = 3y2

  3. What are functions? • Functions are operations. • They have INPUT and OUTPUT. x = sin(y) input output function

  4. Functions in C • Some “functions” in C are built-in: x = sin (y); (Actually “sin” isn’t built-in, but it’s part of math.h, which you always #include.)

  5. Functions in C • Other built-in functions aren’t necessarily equal to anything: printf (“Hello!\n”); input no “output” except to the screen function

  6. Functions in C • What’s useful about functions in C is that you can write your own functions– pieces of code that can be used over and over again with different input! • A good example– converting Fahrenheit to Celsius…

  7. FtoC • We’ll write a function that converts temperature in Fahrenheit to Celsius. • TempC = FtoC(TempF); input output function

  8. FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } Here is an example of a function in C. Notice it is a part of your program, between the #includes and the main() program.

  9. FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } Functions are “declared”, just like a variable would be. Functions can be “float”, “int”, etc…

  10. FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } The “arguments” (or “parameters”) of the function need to be declared as well, so that the function knows what kind of data will be the input.

  11. FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } Inside the function, code works just like in the main program. You can call other functions inside of this function.

  12. FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } Whatever follows the “return” is “returned” to the main program.

  13. Running FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } When the program runs, it will start at the top of the main function. (Yes, “main” is a FUNCTION!)

  14. Running FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } Execution continues normally until you reach the line circled.

  15. Running FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } C has no idea what this “FtoC” is, so it looks to see if it is a function, and it is! The VALUE of tempF is “passed” to FtoC.

  16. Running FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } Inside FtoC, the VALUE passed to FtoC is called “tF”. This VALUE is used inside FtoC to compute tC. tC is “returned” to the main program.

  17. Running FtoC #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); } So now tempC is set equal to the returned value from FtoC. Now we could printf this value, use it in further calcuations, etc.

  18. Why Use Functions? #include <stdio.h> #include <math.h> float FtoC (float tF ) { float tC; tC = (tF – 32.0) * (5./9.); return tC; } main () { float tempC, tempF; tempF = 98.6; tempC=FtoC(tempF); printf (“%f\n”,FtoC(10.)); } • Functions are “reusable”: • The same function is used a second time in this program to printf the value of 10°F converted to Celsius.

  19. Void Functions #include <stdio.h> #include <math.h> void printavalue (float x ) { printf (“The value is %f.\n”,x); } main () { float value1, value2; value1 = 3.14159; value2 = 286.1; printavalue(value1); printavalue(value2); } • Not all functions return anything– they might just be a calculation or a piece of reusable code. • Declare these as “void”.

  20. Void Functions #include <stdio.h> #include <math.h> void printavalue (float x ) { printf (“The value is %f.\n”,x); } main () { float value1, value2; value1 = 3.14159; value2 = 286.1; printavalue(value1); printavalue(value2); } • There is no “return” statement in a void function.

  21. Void Functions #include <stdio.h> #include <math.h> void printavalue (float x ) { printf (“The value is %f.\n”,x); } main () { float value1, value2; value1 = 3.14159; value2 = 286.1; printavalue(value1); printavalue(value2); } • Notice that nothing in the main program “equals” these functions!

  22. Multiple Inputs #include <stdio.h> #include <math.h> float area (float width, float height ) { float A; A = width * height; return A; } main () { float x,y; x = 10.; y = 15.; printf (“The area of a %f m x %f m rectangle is %f m^2.\n”,x,y,area(x,y)); } Functions can have multiple sources of input, such as in this example which computes the area of a rectangle.

  23. Multiple Inputs #include <stdio.h> #include <math.h> float area (float width, float height ) { float A; A = width * height; return A; } main () { float x,y; x = 10.; y = 15.; printf (“The area of a %f m x %f m rectangle is %f m^2.\n”,x,y,area(x,y)); } Notice that both of the inputs to the function have to be declared separately!

  24. Multiple Inputs #include <stdio.h> #include <math.h> float area (float width, float height ) { float A; A = width * height; return A; } main () { float x,y; x = 10.; y = 15.; printf (“The area of a %f m x %f m rectangle is %f m^2.\n”,x,y,area(x,y)); } In this case, notice that there is still just one output (A) which is returned to the main program (and, in this case, printed).

  25. Your Assignment • Write one program that contains functions that perform the following operations: • FtoC (converts Fahrenheit to Celsius) • CtoF (converts Celsius to Fahrenheit) • CtoK (converts Celsius to Kelvin) • KtoC (converts Kelvin to Celsius) • mbtoPa (converts millibars to Pascals) • Patomb (converts Pascals to millibars) • more….

  26. Your Assignment • More functions for your program: • wchill (computes wind chill from temperature and wind speed) • pottemp (computes the potential temperature from the pressure and the temperature) • clausiusclap (computes saturation vapor pressure given the temperature, or vapor pressure given the dewpoint) • RH (computes the relative humidity, given vapor pressure and saturation vapor pressure) • More…

  27. Your Assignment • The main function of your program should use (“call”) these functions to solve these problems: • If the temperature is 265K, what is the temperature in Fahrenheit? • If the dewpoint is 55°F, what is the dewpoint in Kelvin? • If the pressure is 1013.25mb, what is the pressure in Pascals? • More…

  28. Your Assignment • If the pressure is 54000 Pascals, what is the pressure in millibars? • If the temperature is 10°C and the wind is at 35 knots, what is the wind chill in Celsius? • If the temperature is 10°F and the wind is at 35 knots, what is the wind chill in °F? • What is the potential temperature of air at 20°C and a pressure of 850mb? • What is the relative humidity of air at a temperature of 55°F and a dewpoint of 40°F?

More Related