Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.

Slides:



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

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
CSE 303 Lecture 16 Multi-file (larger) programs
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C Programming Lecture 7 Functions. Structured Programming b Keep the flow of control in a program as simple as possible. b Use top-down design. Keep decomposing.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
1 Functions (covered by Chapter 5 in ABC). 2 Type function_name( parameter list ) { Declarations Statements } Function Definition Header body Partitioning.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Chapter 4 (II) Functions and Structured Programming.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 Chapter 8 Functions, Pointers, and Storage Classes  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5.
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Functions, locals, parameters, and separate compilation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
A First Book of ANSI C Fourth Edition
Dynamic memory allocation and Intraprogram Communication.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Functions Course conducted by: Md.Raihan ul Masood
C Functions -Continue…-.
Functions, locals, parameters, and separate compilation
Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programmazione I a.a. 2017/2018.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
Dynamic memory allocation and Intraprogram Communication
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Functions and Program Structure
Chapter 6 - Functions Outline 5.1 Introduction
6 Chapter Functions.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Department of Statistics St. Joseph’s College (Autonomous)
Functions.
Presentation transcript:

Functions

Flow of Control Review while for do while goto break & continue switch Relational operators short circuits

Functions To avoid repetitive similar code To structure the whole program as “top-down” approach –Breaking up a large problem into smaller pieces and break each piece into smaller ones until each piece is readily expressed in code –Each piece should be concise and logical entity

header body

Function Definition function type –Type of the return value, if any –If missing, it is assumed to be int parameters are placeholders for values that are passed to the function when it is invoked return statements return; return a+b; return (a+b);

Function Prototypes Each function should be declared before it is used –what if your program structure is top down? –some people prefer bottom-up approaches Usually, they are placed before the main( ) function Parameter names can be omitted (ANSI C) –parameters can be omitted (old C)

#include #define N 7 long power(int, int); void prn_heading(void); void prn_tbl_of_powers(int); int main(void) { prn_heading(); prn_tbl_of_powers(N); return 0; } void prn_heading(void) { printf("\n::::: A TABLE OF POWERS :::::\n\n"); } void prn_tbl_of_powers(int n) { int i, j; for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) if (j == 1) printf("%ld", power(i, j)); else printf("%9ld", power(i, j)); putchar('\n'); } long power(int m, int n) { int i; long product = 1; for (i = 1; i <= n; ++i) product *= m; return product; }

Function Invocation The program starts by invoking the main function parameters are passed as call-by-value –you can implement call-by-reference with pointers

#include int main(void) { int n = 3, sum, compute_sum(int); printf("%d\n", n); /* 3 is printed */ sum = compute_sum(n); printf("%d\n", n); /* 3 is printed */ printf("%d\n", sum); /* 6 is printed */ return 0; } int compute_sum(int n) /* sum the integers from 1 to n */ { int sum = 0; for ( ; n > 0; --n) /* stored value of n is changed */ sum += n; return sum; }

Developing a Large Program Usually developed by several teams Comprises many.h and.c files –each.c file can be compiled separately gcc –o pgm main.c fct.c wrt.c

Assertion you can make sure a certain condition holds true at any place of program –it is a macro defined in the header file assert.h –assert(expression); –if the value of the expression is zero abort the program

Scope Rules identifiers are accessible only within the block where they are defined –they are invisible from outside

Storage Classes Every variable and functions in C has two arrributes: type and storage class Storage Classes autoexternregisterstatic

auto the most common class –variables defined inside a function –variables defined outside a function are global default class – you may omit it the memory space is allocated/released when the function is invoked/exited when a function is reentered, the previous values are unknown

external global they may be defined somewhere else (in another file) they never disappear –transmit values across functions they may be hidden by re-declaration, but they are not destroyed

CC = gcc CFLAGS = -Wall EXEC = a.out INCLS = LIBS = OBJS = main.o fct.o $(EXEC): $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS) $(OBJS): $(CC) $(CFLAGS) $(INCLS) -c $*.c $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS) #include int a = 1, b = 2, c = 3; /* external variables */ int f(void); int main(void) { printf("%3d\n", f( ) ); printf("%3d%3d%3d\n", a, b, c); return 0; } int f(void) { extern int a; /* look for it elsewhere */ int b, c; a = b = c = 4; return (a + b + c); } fct.c main.c

register allocate this variable on a register to speed up the execution not always possible to find a register tricky for memory-IO operations

static to preserve the value even after the function exits –extern does the same to control visibility of variable and functions –“static extern” - visible only within the same source file void f(void) { static int cnt = 0; ++cnt; if (cnt %2 == 0)..... else.... } static int seed = 100; /* static extern – external, but invisible from other files */ int random(void) { seed = * seed ;.... }

/* function g( ) can be seen only within this file */ static int g(void) {.... } void f(int a) { }