CS201 – C Functions Procedural Abstraction Code Reuse C Library.

Slides:



Advertisements
Similar presentations
CSE 303 Lecture 16 Multi-file (larger) programs
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Chapter 6: User-Defined Functions I
1 CS 201 Exam 1 Review More on Makefiles Debzani Deb.
Overview creating your own functions calling your own functions.
Modular Programming From Chapter 6. Output Parameters Pass by reference. In the calling program: int frog, lily; int * frog_ptr=&frog; function_name(frog_ptr,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
chap13 Chapter 13 Programming in the Large.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 CS161 Introduction to Computer Science Topic #10.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Learners Support Publications Functions in C++
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
User-Written Functions
Chapter 6: User-Defined Functions I
Separate Compilation and Namespaces
Functions Separate Compilation
Pre-processor Directives
User-Defined Functions
Separate Compilation and Namespaces
User Defined Functions
Comments, Prototypes, Headers & Multiple Source Files
CS149D Elements of Computer Science
In C Programming Language
C Preprocessor Seema Chandak.
Methods/Functions.
Templates CMSC 202, Version 4/02.
CPS125.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

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); 1 st void means no value returned 2 nd 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 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

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; ……

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.cfcn2.cfcn1.cfcnn.c proga.ofcn1.ofcn2.ofcnn.c gcc -c library proga gcc -o

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