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

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

Embed Size (px)

Citation preview

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

1

Structure of a C Program

(continued)

Presentation original from Dr. Turner’s class

USF - COP-2270 - C for Engineers Summer 2008

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

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”

2USF - COP-2270 - C for Engineers

Summer 2008

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

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.

3USF - COP-2270 - C for Engineers

Summer 2008

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

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.

4USF - COP-2270 - C for Engineers

Summer 2008

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

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

5USF - COP-2270 - C for Engineers

Summer 2008

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

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.

6USF - COP-2270 - C for Engineers

Summer 2008

void say_hello(){ printf ("Hello, world\n");}

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

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.

7USF - COP-2270 - C for Engineers

Summer 2008

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

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.

8USF - COP-2270 - C for Engineers

Summer 2008

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

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

9USF - COP-2270 - C for Engineers

Summer 2008

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

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.

10USF - COP-2270 - C for Engineers

Summer 2008

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

Example: A Function that Computes a Value#include <stdio.h>

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

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

12

Run It!

12USF - COP-2270 - C for Engineers

Summer 2008

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

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.

13USF - COP-2270 - C for Engineers

Summer 2008

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

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.

14USF - COP-2270 - C for Engineers

Summer 2008