CS110_02c_functions - EECS110: 2c y = Functions(x); Jack...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 2c y = Functions(x); Jack Tumblin jet@cs.northwestern.edu . Recall: Expressions Statements ; grp = (((n+5)* a) / q) A `statement' in C is: zero or more expression(s) terminated by a semicolon ; For each expression (each red circle), C makes a (hidden) temporary variable (Recall: some expressions have side-effects) Semicolon means: `Don Recall: Expressions Statements ; grp = (((n+5)* a) / q) d1 d2 d3 d4 A `statement' in C is: zero or more expression(s) terminated by a semicolon ; For each expression (each red circle), C makes a (hidden) temporary variable (that's WHY some expressions have "side-effects") Semicolon means: `Done! Destroy all temporary variables!' Expressions Statements Functions Small programs are simpler -big programs get complicated, tangled Key CS idea: `Decomposition' or `Nesting': break big complicated sets of instructions into smaller, simpler chunks `a block of statements' { statement1; statement2; ... In C, these chunks are called functions: a separate, named block of statements that you can just call by name to run } Functions You Know Already main() : The first (and last) function executed when the program runs. printf("%f \n", in); scanf(" %d",&in); ... Nesting of functions allowed (and encouraged!): from within main() you can invoke or "call" other functions. Your own functions can call others, and so on. How to Use Existing Functions 1) Use `include' directives to tell C to get the file(s) that hold the functions: #include <stdio.h> // #include<X> == "search the usual places for file X" #include "coatrack.h" // #include "X" == "search this directory for file X" 2) Call them by name in your program: ... scanf(" %d, %d",hats,coats); isFull = hatChek(hats,coats); A Function... Performs one well-defined task (e.g. printf) Is re-usable by other programs Is a "black box", a mini-program; One simple set of inputs, (variables) One simple output, One hidden body of protected statements You can improve & debug a function body without affecting the rest of the program! Example: Without Functions #include<stdio.h> int main(void) // Farenheit-to-Celsius { float degF,degC, ratio; printf("Enter degrees F: "); scanf(" %f", &degF); // F-to-C conversion: ratio = 100.0f / (212.0f-32.0f); // We will unify this degC = (degF-32)*ratio; // set of statements to // make a new function. printf("%f degrees F are %f degrees C\n",degF,degC); return 0; } What if we need this conversion step in many different places in our program? Example: With Functions #include<stdio.h> ALWAYS follow these 3 Steps: Steps 1. Declare the function (CS Jargon: this statement is the `Function prototype') float F_to_C (float far); int main(void) { float degF,degC; 3. Call the function printf("Enter degrees F: "); whenever you need it scanf("%f", &degF); degC = F_to_C(degF); printf("%f degrees F are %f degrees C\n",degF,degC); return 0; } float F_to_C (float far) { const float ratio = 100.0f/(212.0f 32.0f); return (far-32)*ratio; } 2. Define the function CS jargon: this is the Function Body Function Essentials A function has: a unique name; use the name to run the function a body; one block of statements and variables, some (optional) inputs; a list of `arguments' one (optional) output; a single `return value' Just like variables, all functions must be "declared" before you use them Also must "define" functions--write the body How to Write a New Function 1) Declare the function: write a `function prototype' to permit C to: Find your function by its name, Supply it with all required inputs, Capture and use its output value (if any) 2) Define the function: write the named block of statements. 3) Call them by name in your program: Details: Function Declaration Write a 3-part `function prototype' to define its... Name: choose wisely--well worth the time spent on it! Inputs (if any): an argument list made of input variables' names and data types; Return type data type of returned value, if any (default: int) Example: int make_tea(float water, int teabags ); Where? Put prototypes after #include statements, but before main() begins. Declaration and Definition #include<stdio.h> function name /* function prototype */ float F_to_C (float far); int main(void) return type argument list (one or more inputs) { (for output) float degF,degC; printf("Enter degrees F: "); scanf("%f", &degF); degC = F_to_C (degF); printf("%f degrees F are %f degrees C\n",degF,degC); return 0; } float F_to_C (float far) { const float ratio = 5.0 / 9.0; function definition return (far-32)*ratio; } (or `function body') Formally: Function Syntax Syntax of a function prototype: return_type function_name(argument_list); (don't forget semicolon!) Syntax of a function: return_type function_name(argument_list) { statements; (Almost the same) } If a function does not return a value, use return type void. Functions can have their own variables, just as main() does. return stops a function & sets its output value. Functions use Local variables... Variables declared inside a function are meaningful only within that function A GREAT idea--one of the best in all of CS! Two different functions can use the same names for their local variables, without conflict. Let different programmers write different functions: no `coordinated' var. names needed! Example: local variables float grow_val(float x); main () { float in,out; /* fcn. prototype */ Same Variable names?!? --its OK; they're local only in = -4.3; out = grow_val(in); printf("%f gives %f\n",in,out); } float grow_val(float xmax) /*fcn. definition */ { float in; in = xmax*2.1f + ((int)xmax)%5; // (doesn't matter) return(in*(xmax-1.0) + xmax); } Global variables: Not inside a Function Applies to ALL parts of a program Declared before the main function When a global variable is changed, the new value is available anywhere in the program. Global variables are !! A VERY BAD IDEA !! Prevents splitting your problem into separate pieces Ensures debugging nightmares Is (almost) NEVER necessary! Function Calls: Arguments, Formal Parameters Look again at a function prototype: void nextChar( char ch); // prototype char nextChar(char ch); What does this mean?!? Function declarations make local variables or `formal parameters' valid only inside your function Calling a function with arguments assigns their values (copies them) to the `formal parameters' int main() { char strt = `J'; char end; end = nextChar(strt); printf("%c%c",strt,end); return 0; } char nextChar(char ch) { return (ch +1); } ...
View Full Document

Ask a homework question - tutors are online