1 / 21

CS201 – C Functions

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

baba
Download Presentation

CS201 – C Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS201 – C Functions Procedural Abstraction Code Reuse C Library

  2. 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

  3. 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

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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)

  9. 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.

  10. 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.

  11. 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

  12. 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

  13. 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.)

  14. 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

  15. #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.

  16. 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

  17. 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!

  18. 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.

  19. 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.

  20. 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

  21. 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

More Related