1 Functions and Program Structure C Foundation. 2 f'n declarations  a promise that a function exists  typically written in header files  return type.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
Programming Languages and Paradigms The C Programming Language.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Chapter 7: User-Defined Functions II
C Programming - Lecture 5
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Structure of a C program
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
18. DECLARATIONS.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
File IO and command line input CSE 2451 Rong Shi.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Advanced BioPSE NCRR Programming Conventions and Standards J. Davison de St. Germain Chief Software Engineer SCI Institute December 2003 J.
A First Book of ANSI C Fourth Edition
C++ Functions A bit of review (things we’ve covered so far)
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
C++ Lesson 1.
Function: Declaration
Chapter 6 CS 3370 – C++ Functions.
A bit of C programming Lecture 3 Uli Raich.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Functions and Structured Programming
Functions Separate Compilation
C Short Overview Lembit Jürimägi.
Instructor: Ioannis A. Vetsikas
Chapter 5 - Functions Outline 5.1 Introduction
Built-In (a.k.a. Native) Types in C++
Govt. Polytechnic,Dhangar
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Homework Finish up K&R Chapters 3 & 4
Programming Language C Language.
C Programming - Lecture 5
Building Blocks of C Programming Language
Functions Reasons Concepts Passing arguments to a function
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
The Three Attributes of an Identifier
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
INTRODUCTION TO C.
Presentation transcript:

1 Functions and Program Structure C Foundation

2 f'n declarations  a promise that a function exists  typically written in header files  return type must be explicit  use void for no parameters and no return value... FILE * fopen(const char * path, const char * mode); int fflush(FILE * stream);... void perror(const char * diagnostic); FILE * tmpfile(void);... FILE * fopen(const char * path, const char * mode); int fflush(FILE * stream);... void perror(const char * diagnostic); FILE * tmpfile(void);... stdio.h parameter names are optional but help readability and documentation 1. f(int a, int b) cannot be shortened to f(int a, b); 2. avoid bool parameters as they can be confusing on their own

3 header files  can contain  #includes  macro guards (idempotent)  macros (e.g. EOF)  type declarations (e.g. FILE)  external data declarations (e.g. stdin)  external function declarations (e.g. printf)  inline function definitions  should not contain  function definitions (unless inline)  data definitions

4 header example #ifndef STDIO_INCLUDED #define STDIO_INCLUDED #define EOF (-1) typedef struct FILE FILE; extern FILE * stdin; FILE * fopen(const char * path, const char * mode); int fflush(FILE * stream);... void perror(const char * diagnostic); FILE * tmpfile(void);... #endif #ifndef STDIO_INCLUDED #define STDIO_INCLUDED #define EOF (-1) typedef struct FILE FILE; extern FILE * stdin; FILE * fopen(const char * path, const char * mode); int fflush(FILE * stream);... void perror(const char * diagnostic); FILE * tmpfile(void);... #endif stdio.h # IF Not DEFined all uppercase: a strong convention

5 definitions  a definition honours the promise  written in source files #include struct FILE {... }; FILE * fopen(const char * path, const char * mode) {... } int fflush(FILE * stream) {... } void perror(const char * message) {... } #include struct FILE {... }; FILE * fopen(const char * path, const char * mode) {... } int fflush(FILE * stream) {... } void perror(const char * message) {... } stdio.c parameter name can be different to that used in the function declaration parameter names are required

6 return statement  the return statement returns a value!  the expression must match the return type  either exactly or via implicit conversion  to end a void function early use return; return expression opt ; const char * day_suffix(int days) { const char * result = "th"; if (days / 10 != 1) switch (days % 10) {... } return result; } const char * day_suffix(int days) { const char * result = "th"; if (days / 10 != 1) switch (days % 10) {... } return result; }

7 pass by pointer  use a pointer to a non-const  if the definition needs to change the target... void delay( date * when);... void delay( date * when);... delay.h #include "delay.h" int main(void) { date due = { 2008, april, 10 }; delay(&due);... } #include "delay.h" int main(void) { date due = { 2008, april, 10 }; delay(&due);... } the lack of a const here means delay might change *when date * when = &due;

8 pass by value  changing the parameter does not change the argument bool search( const int values[], size_t from, size_t to, int find) { while (from != to && values[from] != find) { from++; } return from != to; } bool search( const int values[], size_t from, size_t to, int find) { while (from != to && values[from] != find) { from++; } return from != to; } int main(void) { search(array, 0, size, 42);... } int main(void) { search(array, 0, size, 42);... } size_t from = 0; size_t to = size;

9 pass by value  works for enums and structs too  but not for arrays (unless the array is inside a struct) date.h #include "date.h"... int main(void) { date today = { 2008, april, 10 };... puts(dayname(today)); assert(today.year == 2008); assert(today.month == april); assert(today.day == 10); } #include "date.h"... int main(void) { date today = { 2008, april, 10 };... puts(dayname(today)); assert(today.year == 2008); assert(today.month == april); assert(today.day == 10); }... const char * dayname(date when);... const char * dayname(date when);... Thursday date when = today;

10 pass by ptr to const  an alternative to pass by copy  except that the parameter can be null... const char * dayname(const date * when);... const char * dayname(const date * when);... date.h the const here promises that dayname wont change *when #include "date.h"... int main(void) { date today = { 2008, april, 10 };... puts(dayname(&today)); assert(today.year == 2008); assert(today.month == april); assert(today.day == 10); } #include "date.h"... int main(void) { date today = { 2008, april, 10 };... puts(dayname(&today)); assert(today.year == 2008); assert(today.month == april); assert(today.day == 10); } const date * when = &today; Thursday

11 parameter advice  pass by plain pointer...  when the function needs to change the argument  pass by copy for built in types and enum...  they are small and they will stay small  copying is supported at a low level  very fast  for most structs...  mimic pass by copy with pointer to const  they are not small and they only get bigger!  very fast to pass, but be aware of indirection cost  for some structs...  pass by copy can be OK  but make sure they are both small and stable

12 top level const?  makes no sense on a function declaration const char * day_suffix(const int days) {... } const char * day_suffix(const int days) {... } const char * day_suffix(const int days); the const here (on a function definition) is reasonable and states that the function will not change days the const here is meaningless write this instead const char * day_suffix(int days); ? ?

13 auto variables  the default storage class for local variables  life starts on entry to the block  life ends on exit from the block  does not have a default initial value  use if indeterminate causes undefined behaviour size_t count(const int values[], size_t size, int find) { size_t total = 0;... } size_t count(const int values[], size_t size, int find) { size_t total = 0;... } you write this compiler sees as this (legal but massively non-idiomatic) size_t count(const int values[], size_t size, int find) { auto size_t total = 0;... } size_t count(const int values[], size_t size, int find) { auto size_t total = 0;... } ???

14 register variables  a speed optimization hint to the compiler  compiler will use registers as best it can anyway  effect is implementation defined  register variables can't have their address taken void send(register short * to, register short * from, register int count) { register int n = (count + 7) / 8; switch (count % 8) { case 0 : do { *to++ = *from++; case 7 : *to++ = *from++; case 6 : *to++ = *from++; case 5 : *to++ = *from++; case 4 : *to++ = *from++; case 3 : *to++ = *from++; case 2 : *to++ = *from++; case 1 : *to++ = *from++; } while (--n > 0); } void send(register short * to, register short * from, register int count) { register int n = (count + 7) / 8; switch (count % 8) { case 0 : do { *to++ = *from++; case 7 : *to++ = *from++; case 6 : *to++ = *from++; case 5 : *to++ = *from++; case 4 : *to++ = *from++; case 3 : *to++ = *from++; case 2 : *to++ = *from++; case 1 : *to++ = *from++; } while (--n > 0); } ??

15 static local variables  a local variable with static storage class  a local variable with 'infinite' lifetime  best avoided – subtle and hurts thread safety  but ok for naming magic numbers (as are enums)  and often useful in unit test mock functions int remembers(void) { static int count = 0; return ++count; } int remembers(void) { static int count = 0; return ++count; } ? void send(short * to, short * from, int count) { static const int unrolled = 8; int n = (count + unrolled - 1) / unrolled; switch (count % unrolled) {... } void send(short * to, short * from, int count) { static const int unrolled = 8; int n = (count + unrolled - 1) / unrolled; switch (count % unrolled) {... }

16 recursion  a function can call itself  directly or indirectly  can often be rewritten using a loop int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n – 1); } int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n – 1); } this will cause integer overflow for quite small values of n ?

17 linking  a linker links the use of an identifier in one file with its definition in another file  an identifier is made available to the linker by giving it external linkage (the default) using the extern keyword  an identifier is hidden from the linker by giving it internal linkage using the static keyword use use internal external  linker error definitions

18 function linkage  function declarations  default to external linkage  extern keyword makes default explicit... struct tm * localtime(const time_t * when); time_t time(time_t * when);... struct tm * localtime(const time_t * when); time_t time(time_t * when);... time.h extern struct tm * localtime(const time_t * when); extern time_t time(time_t * when);... extern struct tm * localtime(const time_t * when); extern time_t time(time_t * when);... equivalent time.h ?

19 function linkage  function definitions  default to external linkage  use static keyword for internal linkage static void hidden(time_t * when); static void hidden(time_t * when) {... } static void hidden(time_t * when); static void hidden(time_t * when) {... } source.c time_t time(time_t * when) {... } time_t time(time_t * when) {... } time.c extern time_t time(time_t * when) {... } extern time_t time(time_t * when) {... } equivalent time.c ?

20 data linkage  without a storage class or an initializer  the definition is tentative – and can be repeated  this is confusing and not compatible with C++  recommendation: extern data declarations  use explicit extern keyword, do not initialize  recommendation: extern data definitions  do not use extern keyword, do initialize int v; // external, tentative definition... int v; // external, tentative definition... int v; // external, tentative definition ok in ISO C, duplicate definition errors in C++ extern int v; multiple declarations ok int v = 42; single definition with initializer ?

21 type linkage?  static can be used on a type definition  it has no affect - type names do not have linkage  don't do it static struct wibble {... }; static enum fubar {... }; static struct wibble {... }; static enum fubar {... }; ??

22 inline functions  default to external linkage  creates a requirement for an "external" definition in another translation unit  simplest approach is to give all inline functions internal linkage  does not affect sequence point model – there is still a sequence point before a call to an inline function #include static inline bool is_even(int value) { return value % 2 == 0; } #include static inline bool is_even(int value) { return value % 2 == 0; } is_even.h

23 __func__  the name of the current function is available  via the reserved identifier __func__ void some_function(void) { puts(__func__); } void some_function(void) { puts(__func__); } void some_function(void) { static const char __func__[] = "some_function"; puts(__func__); } void some_function(void) { static const char __func__[] = "some_function"; puts(__func__); } as-if compiler translation 

24 variadic functions  functions with a variable no. of arguments  helpers in provide type-unsafe access int printf(const char * format,...); #include int my_printf(const char * format,...) { va_list args; va_start(args, format); for (size_t at = 0; format[at] != '\0'; at++) { switch (format[at]) { case 'd': case 'i': print_int(va_arg(format, int)); break; case 'f': case 'F': print_double(va_arg(format, double)); break;... } va_end(args); } #include int my_printf(const char * format,...) { va_list args; va_start(args, format); for (size_t at = 0; format[at] != '\0'; at++) { switch (format[at]) { case 'd': case 'i': print_int(va_arg(format, int)); break; case 'f': case 'F': print_double(va_arg(format, double)); break;... } va_end(args); }

25 pointers to functions  in an expression the name of function "decays" into a pointer to the function  ( ) is a binary operator with very high precedence  f(a,b,c)  f () {a,b,c}  you can name a function without calling it!  the result is a strongly typed function pointer #include int add(int a, int b) { return a + b; } int sub(int a, int b) { return a – b; } int main(int argc, char * argv[]) { int (*f)(int,int) = argc % 2 == 0 ? add : sub; printf("%d\n", f(4, 2)); } #include int add(int a, int b) { return a + b; } int sub(int a, int b) { return a – b; } int main(int argc, char * argv[]) { int (*f)(int,int) = argc % 2 == 0 ? add : sub; printf("%d\n", f(4, 2)); } * needed here

26 function ptr args  function pointers can be function parameters!  * is optional on the parameter #include int add(int a, int b) { return a + b; } int sub(int a, int b) { return a – b; } int call(int f(int,int)) { return f(4, 2); } int main(int argc, char * argv[]) { int (*f)(int,int) = argc % 2 == 0 ? add : sub; printf("%d\n", call(f)); } #include int add(int a, int b) { return a + b; } int sub(int a, int b) { return a – b; } int call(int f(int,int)) { return f(4, 2); } int main(int argc, char * argv[]) { int (*f)(int,int) = argc % 2 == 0 ? add : sub; printf("%d\n", call(f)); } int call(int (*f)(int,int)) { return (*f)(4, 2); } int call(int (*f)(int,int)) { return (*f)(4, 2); }

27 summary  never define data in a header file  mimic pass by copy using pointer to const for structs  never use the auto keyword  never use the register keyword  use static local variables only if const  give static linkage to anything in a source file not declared in its header

28 more…?  function returning pointer to function returning int #include int add(int a, int b) { return a + b; } int sub(int a, int b) { return a – b; } int (*choose(int argc))(int, int) { return argc % 2 == 0 ? add : sub; } #include int add(int a, int b) { return a + b; } int sub(int a, int b) { return a – b; } int (*choose(int argc))(int, int) { return argc % 2 == 0 ? add : sub; }

29 more…?  function calls – default promotions  (see notes)

30 complete types  mentioning type T in a function declaration does not require sizeof(T) information  even if T is a value return/parameter #include "fubar.h" #include "snafu.h" typedef struct wibble { snafu s; fubar f; } wibble; wibble value_return(void); wibble * ptr_return(void); void value_parameter(wibble); void ptr_parameter(wibble *); #include "fubar.h" #include "snafu.h" typedef struct wibble { snafu s; fubar f; } wibble; wibble value_return(void); wibble * ptr_return(void); void value_parameter(wibble); void ptr_parameter(wibble *);

31 incomplete types  mentioning type T in a function declaration does not require sizeof(T) information  Function Declarators para 12 If the function declarator is not part of a definition of that function, parameters may have incomplete types… typedef struct wibble wibble; wibble value_return(void); wibble * ptr_return(void); void value_parameter(wibble); void ptr_parameter(wibble *); typedef struct wibble wibble; wibble value_return(void); wibble * ptr_return(void); void value_parameter(wibble); void ptr_parameter(wibble *); no #includes required