Programming Fundamentals Lecture #7 Functions

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
 2006 Pearson Education, Inc. All rights reserved Functions.
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.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 6: User-Defined Functions
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
UMBC CMSC 104 – Section 01, Fall 2016
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Introduction to Programming
IS Program Design and Software Tools Introduction to C++ Programming
Functions and an Introduction to Recursion
C Functions -Continue…-.
Methods Chapter 6.
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
6 Functions.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 2 of 3 Topics Functions That Return a Value
6 Functions.
Assignment Operators Topics Increment and Decrement Operators
CS2011 Introduction to Programming I Methods (II)
Assignment Operators Topics Increment and Decrement Operators
Chapter 9: Value-Returning Functions
Function.
1-6 Midterm Review.
Assignment Operators Topics Increment and Decrement Operators
Function.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Presentation transcript:

Programming Fundamentals Lecture #7 Functions Junaid Hassan Lecturer CS & IT Department UOS MBDIN junaidte14@gmail.com

Functions Objectives: To construct programs modularly from small pieces called functions Common math functions in C standard library How to create new functions The mechanism used to pass information between functions How to write and use functions that call themselves Function definitions, Function call stack and activation records, Headers, Calling functions by values and by reference, scope rules, Recursion, Fibonacci series, Recursion vs iteration

Headers Each standard library has a corresponding header containing the function prototypes for all the functions in that library, as well as definitions of various symbolic constants needed by those functions e.g <ctype.h> Contains function prototypes for functions that test characters for certain properties, and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa <errno.h> Defines macros that are useful for reporting error conditions <math.h> Contains function prototypes for math library functions

Headers <stdio.h> Contains function prototypes for the standard input/output library functions, and information used by them <stdlib.h> Contains function prototypes for conversions of numbers to text and text to numbers, random numbers, and other utility functions <string.h> Contains function prototypes for string-processing functions. <time.h> Contains function prototypes and types for manipulating the time and date

Calling Functions By Value & By Reference There are two ways to invoke functions in many programming languages—call-by-value and call-by-reference In call by value a copy of actual arguments is passed to respective function arguments. While, in call by reference the location (address) of actual arguments is passed to function arguments In C, all function arguments are passed "by value" because C does not support references like C++ and Java do

Calling Functions By Value & By Reference Call By Value method does not affect the original variables e.g void swap(int n1, int n2); int a = 5; int b = 10; swap(a, b); void swap(int n1, int n2){ int temp = n1; n1 = n2; n2 = temp; } In this example code values of a and b are not changed when they are passed to the swap function.

Calling Functions By Value & By Reference Call By Reference method may affect the original variables e.g void swap(int *n1, int *n2); int a = 5; int b = 10; swap(&a, &b); void swap(int *n1, int *n2){ int temp = n1; n1 = n2; n2 = temp; } In this example code values of a and b are changed when they are passed to the swap function.

Scope Rules The scope of an identifier is the portion of the program in which the identifier can be referenced. For example, when we define a local variable in a block, it can be referenced only following its definition in that block or in blocks nested within that block The four identifier scopes are function scope, file scope, block scope, and function- prototype scope

Scope Rules (Function Scope) Labels (an identifier followed by a colon such as start:) are the only identifiers with function scope. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. Labels are used in switch statements (as case labels) This is called information hiding

Scope Rules (File Scope) An identifier declared outside any function has file scope. Such an identifier is “known” (i.e., accessible) in all functions from the point at which the identifier is declared until the end of the file. Global variables, function definitions, and function prototypes placed outside a function all have file scope

Scope Rules (Block Scope) Identifiers defined inside a block have block scope. Block scope ends at the terminating right brace (}) of the block. Local variables defined at the beginning of a function have block scope as do function parameters, which are considered local variables by the function

Scope Rules (Function Prototype Scope) The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned previously, function prototypes do not require names in the parameter list— only types are required. If a name is used in the parameter list of a function prototype, the compiler ignores the name. Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity