350 likes | 565 Views
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:
E N D
Functions in C ATS 315
What are functions? • In math, you see functions all the time: x = sin(y) x = 3y2
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: x = sin (y); (Actually “sin” isn’t built-in, but it’s part of math.h, which you always #include.)
Functions in C • Other built-in functions aren’t necessarily equal to anything: printf (“Hello!\n”); input no “output” except to the screen function
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…
FtoC • We’ll write a function that converts temperature in Fahrenheit to Celsius. • TempC = FtoC(TempF); input output function
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.
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…
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.
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.
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.
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!)
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.
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.
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.
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.
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.
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”.
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.
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!
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.
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!
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).
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….
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…
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…
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?