Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP-2270 - C for Engineers Summer 2008.

Similar presentations


Presentation on theme: "1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP-2270 - C for Engineers Summer 2008."— Presentation transcript:

1 1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP-2270 - C for Engineers Summer 2008

2 2 Functions Our primary tool for coping with complexity. Divide a big program into smaller pieces Each piece performs a well defined action Pieces can be further divided as needed Final result is nothing but small, understandable pieces. “Program Design” 2 USF - COP-2270 - C for Engineers Summer 2008

3 Functions Avoid duplicating code. If the same code is needed in more than one place, make it a function. Call the function where that code is needed. 3 USF - COP-2270 - C for Engineers Summer 2008

4 4 Functions Two kinds of functions: Compute a value sin(x) Do something printf() Functions that compute a value must have a return type. Functions that "do something" typically are declared as void. 4 USF - COP-2270 - C for Engineers Summer 2008

5 5 Functions A function is a named block of code that can be executed by putting its name into a statement. A very simple example (Try it!) void say_hello() { printf ("Hello, world\n"); } int main () { say_hello(); return 0; } Function Header Function Body Function “Call” Function Definition Function Name 5 USF - COP-2270 - C for Engineers Summer 2008

6 6 Functions Some things to notice. No semicolon at end of function header. For readability, align the curly brackets with the function header and indent the code within the function body by 4 spaces (The usual rules for alignment of curly brackets.) The function body is delimited by curly brackets. No semicolon at end of function definition. "void" says that the function does not return a value. 6 USF - COP-2270 - C for Engineers Summer 2008 void say_hello() { printf ("Hello, world\n"); }

7 7 Returned Value A function can perform a computation and return the result. The result is the “value” of the function. Function call acts like a variable. Can be used in assignment statement. Can be used in an expression. The function header specifies the type of the returned value. 7 USF - COP-2270 - C for Engineers Summer 2008

8 8 Function Return A function that returns a value must have a return type. int sum (int p1, int p2) { int result; result = p1 + p2; return result; } This function returns an integer value. 8 USF - COP-2270 - C for Engineers Summer 2008

9 9 Function Parameters A function definition can include one or more variables, called parameters, to be supplied by the call. int sum (int p, int p2 ) { int result; result = p1 + p2; return result; } The function call must provide values for function’s parameters. A value passed to the function by the call is referred to as an argument. Each argument becomes the initial value of a parameter when the function is called. Parameters 9 USF - COP-2270 - C for Engineers Summer 2008

10 10 Example: A Function that Computes Return to the earlier example in Visual Studio We will modify it to use a function to do the calculation. 10 USF - COP-2270 - C for Engineers Summer 2008

11 Example: A Function that Computes a Value #include int sum (int p1, int p2) { int result; result = p1 + p2; return result; } int main() { int a; int b; int c; a = 5; b = 7; c = sum(a,b); printf ("The result is %d\n", c); getchar(); /* Keep the window open. */ return 0; } 11 USF - COP-2270 - C for Engineers Summer 2008

12 12 Run It! 12 USF - COP-2270 - C for Engineers Summer 2008

13 13 Function Parameters Parameter names are not significant to the call. Argument values are assigned to parameters in order. Parameters look and act like variables inside the function. Initial values provided by the call. Parameter gets a copy of the value in the call. Variables used in the call are not affected by the function. 13 USF - COP-2270 - C for Engineers Summer 2008

14 14 Assignment Read Hour 2 and Hour 3 in the textbook. Do the examples from this class for yourself (if you didn’t do them in class) Repeat the examples from this class on Unix. If anything doesn't make sense, ask for help. 14 USF - COP-2270 - C for Engineers Summer 2008


Download ppt "1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP-2270 - C for Engineers Summer 2008."

Similar presentations


Ads by Google