Functions.

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.
Chapter 7: User-Defined Functions II
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 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.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
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.
Chapter 6: User-Defined Functions
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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;
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.
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. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Functions Functions, locals, parameters, and separate compilation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
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
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
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
The Three Attributes of an Identifier
Functions, locals, parameters, and separate compilation
Functions.
Pointers.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Command-Line Arguments
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
Instructor: Ioannis A. Vetsikas
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Dynamic memory allocation and Intraprogram Communication
Chapter 5 - Functions Outline 5.1 Introduction
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
Functions and Program Structure
Chapter 6 - Functions Outline 5.1 Introduction
6 Chapter Functions.
Lecture 18 Arrays and Pointer Arithmetic
Chapter 7: User-Defined Functions II
Functions and Program Structure
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Department of Statistics St. Joseph’s College (Autonomous)
Compiler vs linker The compiler translates one .c file into a .o file
The Three Attributes of an Identifier
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Scope Rules.
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 <stdio.h> #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 <stdio.h> int main(void) { int n = 3, sum, compute_sum(int); printf("%d\n", n); /* 3 is printed */ sum = compute_sum(n); 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 auto extern register static

auto the most common class default class – you may omit it 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

#include <stdio.h> main.c #include <stdio.h> 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; } CC = gcc CFLAGS = -Wall EXEC = a.out INCLS = LIBS = OBJS = main.o fct.o $(EXEC): $(OBJS) @echo "linking ..." @$(CC) $(CFLAGS) -o $(EXEC) $(OBJS) $(LIBS) $(OBJS): $(CC) $(CFLAGS) $(INCLS) -c $*.c relink: @echo "relinking ..." fct.c int f(void) { extern int a; /* look for it elsewhere */ int b, c; a = b = c = 4; return (a + b + 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 = 25173 * seed + 13849; .... }

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