Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.

Slides:



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

Introduction to C Programming
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Spring Semester 2013 Lecture 5
Chapter Five Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
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.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
CSCI 171 Presentation 6 Functions and Variable Scope.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Methods Chapter 6 - Methods.
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Chapter 7: User-Defined Functions II
This technique is Called “Divide and Conquer”.
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions I Creating a programming with small logical units of code.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
A First Book of ANSI C Fourth Edition
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
In C Programming Language
Functions I Creating a programming with small logical units of code.
Presentation transcript:

Announcements

Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam Practice

Functions

4 Introducing Functions A C program is constructed from building blocks called functions main() is a function where program execution begins A function is a subroutine that contains one or more C statements and performs one or more tasks

5 Functions … contd. Two types of functions: those written by You and others provided to you in the compiler’s standard library – Library functions (printf, scanf) – User defined

6 Example … library function abs() #include int main() { int x = abs(-10); return 0; } Gives output x

7 Functions … contd. Every function must have a name (main() is reserved) One function cannot be embedded within another function (these are separate entities) One function may call another

8 Function Arguments A value passed to a function is called an argument It is passed by specifying it between the parenthesis that follow the function’s name int x = abs(-10);

9 C function general form Return-type function-name(parameter list) {..body of function. }

10 Function Parameters The variables in a function that receive arguments are called parameters of the function int square(int); int main() { int a = 5; int z = square(a); return 0; } int square(int x) { int z = x *x; return z; } Parameters are local variables of a function

11 Function Function Prototype Function Call Function Definition include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }

12 Function Definitions Function prototype – Tells compiler argument type and return type of function – int square( int ); Function takes an int and returns an int include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }

13 Function Definitions Calling/invoking a function – square(x); – Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments – After finished, passes back result include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }

14 Function Prototypes Function prototype contains – Function name – Parameters (number and data type) – Return type ( void if returns nothing) – Only needed if function definition after function call include int square(int); int main(){ int a = 5; int z = square(a); return 0; } int square(int x){ int z = x *x; return z; }

15 Function Prototypes Prototype must match function definition – Function prototype double maximum( double, double, double ); – Function Definition double maximum( double x, double y, double z ) { … }

16 Function Prototypes If the function definition is after it is called then at least the function prototype should be available before it is called.

17 A function can be declared to return any valid C data type (except that a function cannot return an array) The following are valid: x=power(10,y); if (max(x,y)>100) Returning values from functions

Wrong

Returns one value at a time

Scope Rules for Functions #include void display(int); void main(void){ int i=20; display(i); getch(); } void display(int j){ int k=35; printf("\n%d",j); printf("\n%d",k); } The value of i is not automatically available to function display and has to be passed

Write a function which receives a float and an int from main( ), finds the product of these two and returns the product as a float which is printed through main( ).