CS201 – C Functions

21
CS201 – C Functions Procedural Abstraction Code Reuse C Library

description

CS201 – C Functions. Procedural Abstraction Code Reuse C Library. What is a C function?. Similar to a mathematical function such as g=f(x) Takes input Turn the crank Produces output. x. f. g. Kinds of Functions. No input arguments, no value returned - PowerPoint PPT Presentation

Transcript of CS201 – C Functions

Page 1: CS201 – C Functions

CS201 – C Functions

Procedural AbstractionCode Reuse

C Library

Page 2: CS201 – C Functions

What is a C function?

Similar to a mathematical function such as g=f(x)

Takes input Turn the crank Produces output

x

fg

Page 3: CS201 – C Functions

Kinds of Functions No input arguments, no value returned

Output to some place else (screen) Input arguments, no value returned

Void functions (sub-programs) No input arguments, value returned

Values come from some place else (library)

Input arguments, value returned Normal typed functions

Page 4: CS201 – C Functions

No Inputs, No Value Returned void FunctionName (void);

1st void means no value returned 2nd void means no input arguments

A utility function usually used to output messages to the screen.

Called “void function”. Not many of this kind of function in

normal programs.

Page 5: CS201 – C Functions

Inputs, No value returned void FunctionName (arguments);

void indicates no value returned. arguments are input arguments.

A void function. Gets its data from the arguments. Outputs values some place else. Often used to output data to the

screen.

Page 6: CS201 – C Functions

No Inputs, Value Returned

FunctionType FunctionName (void); FunctionType is type of returned value.

A normal typed function. Gets its data from some place else

(library). Not many of this kind of function in

normal programs.

Page 7: CS201 – C Functions

Inputs, Value Returned

FunctionType FunctionName (arguments); FunctionType is type of returned value. arguments are inputs.

A normal typed function. Gets its data from the argument list. This is the most normal kind of

function.

Page 8: CS201 – C Functions

When do you use a function?

If you find yourself retyping the same code lines more than once.

If you want to share your code with another programmer.

Anytime you need to implement a procedural abstraction. (defer implementation details)

Page 9: CS201 – C Functions

Actual Arguments

The name given to the arguments placed in parentheses after a function call.

Must have values before function is called.

Must agree in number, order, and type of the function definition.

Page 10: CS201 – C Functions

Formal Parameters

Name given to the parameters inside the parentheses after a function definition.

Used to receive values from the calling program.

Must agree in number, order and type of the actual arguments.

Page 11: CS201 – C Functions

Example

nice = MyFunction ( 1, 2.5, 3 );

Actual arguments

Calling Program

Called Function

int MyFunction ( int a, float b, int c )

{ int sum;

sum = a + c;

return (sum);}

Formal parameters

Page 12: CS201 – C Functions

Example

nice = MyFunction ( alpha, beta, chuck );

Actual arguments

Calling Program

Called Function

int MyFunction ( int a, float b, int c )

{ int sum;

sum = a + c;

return (sum);}

Formal parameters

int alpha, chuck;float beta;……

Page 13: CS201 – C Functions

Local Data

All formal parameters and any other variables defined within a function have a value ONLY when the function has been activated.

After the call, these variables become undefined. (There is a way to avoid this, but for now, just think of them as only used during the function use.)

Page 14: CS201 – C Functions

Function Prototypes

Listed in front of the main function to tell the compiler what functions you are planning to use. Type of Function Name of Function Types of Formal Parameters

Page 15: CS201 – C Functions

#include “proto.h”

I like to put all my prototypes in a separate file and then include this file in the main program.

When you do it this way, you need some additional pre-processor directives to avoid duplication.

Page 16: CS201 – C Functions

Sample proto.h

/* proto.h – Function Prototypes */#ifndef _PROTO_H#define _PROTO_Hint fun1(int,int,int);float buildit(float,int);float finalone(int);#endif

Page 17: CS201 – C Functions

Avoids Duplication First it checks to see if the variable

_PROTO_H is defined. If it IS defined, the entire file is skipped. If it ISN’T defined, the first thing done is

to define it. Then the rest of the file is processed. If this file is included twice, the compiler

will only see one copy of it!

Page 18: CS201 – C Functions

Funny Variable Name

_PROTO_H is used so that it never conflicts with any normal variables in any programs.

I usually use the file name since that is pretty unique.

You’ll see this done in all sorts of ways.

Page 19: CS201 – C Functions

Drivers A short piece of code used to test a

function. Passes parameters Receives results Maybe print out a message Print out results of function

As long as you don’t change the interface, the function can be reused.

Page 20: CS201 – C Functions

Using Multiple Source Filesproto.h

proga.c fcn2.cfcn1.c fcnn.c

proga.o fcn1.o fcn2.o fcnn.c

gcc -c gcc -c gcc -c gcc -c

library

proga

gcc -o

Page 21: CS201 – C Functions

Example Makefile# Makefile for multipl file example

# CS201 S'05 Ray S. Babcock

#

fire: fire.o fun1.o fun2.o fun3.o

gcc -o fire fire.o fun1.o fun2.o fun3.o

fire.o: fire.c proto.h

gcc -c fire.c

fun1.o: fun1.c

gcc -c fun1.c

fun2.o: fun2.c proto.h

gcc -c fun2.c

fun3.o: fun3.c

gcc -c fun3.c

clean:

rm *.o fire