250 likes | 382 Views
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
E N D
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 • 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
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.
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.
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.
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.
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)
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.
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.
Example Calling Program Actual arguments nice = MyFunction ( 1, 2.5, 3 ); int MyFunction ( int a, float b, int c ) { int sum; sum = a + c; Formal parameters return (sum); } Called Function
Example Calling Program int alpha, chuck; Actual arguments float beta; …… nice = MyFunction ( alpha, beta, chuck ); int MyFunction ( int a, float b, int c ) { int sum; sum = a + c; Formal parameters return (sum); } Called Function
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.)
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
#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.
Sample proto.h /* proto.h – Function Prototypes */ #ifndef _PROTO_H #define _PROTO_H int fun1(int,int,int); float buildit(float,int); float finalone(int); #endif
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!
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.
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.
Using Multiple Source Files proto.h proga.c fcn1.c fcn2.c fcnn.c gcc -c gcc -c gcc -c gcc -c proga.o fcn1.o fcn2.o fcnn.c gcc -o library proga
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