Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Spring Semester 2013 Lecture 5
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.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Functions:Passing Parameters by Value Programming.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
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.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Functions—Part I. Slide 2 Where are we now? Simple types of variables 3 program structures cin(>>)/cout(
CPS120: Introduction to Computer Science Functions.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CPS120: Introduction to Computer Science Lecture 14 Functions.
 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.
Chapter 7 Functions. Types of Functions Value returning Functions that return a value through the use of a return statement They allow statements such.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
User defined functions
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 9: Value-Returning Functions
User-Written Functions
A Lecture for the c++ Course
Functions Dr. Sajib Datta
9. FUNCTIONS.
Functions in C Mrs. Chitra M. Gaikwad.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
6 Chapter Functions.
Functions.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions.
Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
Arrays .
A function with one argument
Chapter 7: User-Defined Functions II
Chapter 9: Value-Returning Functions
In C Programming Language
Predefined Functions Revisited
CS1100 Computational Engineering
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5

Objectives Understand the concept of functions in C Learn and include function prototype and function definition in C program Know what is global/external and local variables, when to include them in C program Appreciate the purpose of parameter passing and how it works in C program Learn and understand how to incorporate functions in program for validation check Chapter 5 CS12 - Computer Programming 1

Why functions are used in C? to avoid writing the same instructions repeatedly make it easier to organize programs and keep track of what they were doing each subroutine (function) could be written and checked out more or less independently the variables in each subroutine were protected from inadvertent tampering by other subroutines Chapter 5 CS12 - Computer Programming 1

The Structure of Functions There are three program elements involved in using a function: The function definition The function itself is referred to as function definition The call to the function The function is called by main() by using its name including the parenthesis that following the name The function prototype A function is declared in a similar way at the beginning of a program before it is called Chapter 5 CS12 - Computer Programming 1

Chapter 5 CS12 - Computer Programming 1

The Function Definition A function definition has following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> return <expression> } A function returns a single result One of the statements in the function body should have the form: return <expression>; The value passed back by return should have the same type as the function. Chapter 5 CS12 - Computer Programming 1

How to use or call a function? A function call has the following syntax: <function name>(<parameter list>) There is a one-to-one correspondence between the parameters in a function call and the parameters in the function definition. Chapter 5 CS12 - Computer Programming 1

Function Prototype The function prototype has the following syntax: <type> <function name>(<type list>); The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. Chapter 5 CS12 - Computer Programming 1

Local Variables Functions which are known only within that function and other functions are not allowed to use them Also known as automatic variables The length of time a variable lasts is called lifetime. Chapter 5 CS12 - Computer Programming 1

Functions that Return a Value A function that uses no arguments but returns a value is a slightly more complicated kind of function: a function that returns a value. Chapter 5 CS12 - Computer Programming 1

Using Arguments to Pass Data to a Function The mechanism used to convey information to a function is the argument In the function definition, a variable name could be placed in the parentheses Chapter 5 CS12 - Computer Programming 1

Passing Multiple Arguments We can pass as many arguments as we like to function The value of the first actual argument in the calling program is assigned to the first formal argument in the function, and the value of the second actual argument is assigned to the second formal argument Chapter 5 CS12 - Computer Programming 1

‘Void’ type functions A function returns a single result, but a void type function does not return anything (but does something, a sub-program)! Example (void): Printing Cards #include <stdio.h> void printcard(int); int main(){ int c1, c2, c3, c4, c5; printcard(c1); printcard(c2); printcard(c3); printcard(c4); printcard(c5); } void printcard(int cardnum){ if(cardnum==1) Printf("A“); else if(cardnum>=2 && cardnum<=10) Printf(“%d”, cardnum); else if(cardnum==11) Printf("J“); else if(cardnum==12) Printf("Q“); else if(cardnum==13) Printf("K“); } Chapter 5 CS12 - Computer Programming 1

Passing parameters by Value (&) This is by default and desirable behavior! An important fact to remember about parameter passing by value is that changes to the parameters inside the function body have no effect outside of the function. Example: int sum(int a, int b){ a = a + b; return a; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } Chapter 5 CS12 - Computer Programming 1

Passing parameters by reference The value of the parameters passed to a function will also changed in the calling function Example: #include <stdio.h> void Increment(int& Number){ Number = Number + 1; Printf("The parameter Number: %d“, Number); } void main(){ int I = 10; Increment(I); // parameter is a variable Printf("The variable I is: %d\n“, I ); Chapter 5 CS12 - Computer Programming 1

Passing parameters by referenc e Example 2: int main ( ) { int a, b, x = 100; printf("Enter two numbers: "); scanf("%d%d", &a, &b); Add(a, b, x); printf("a, b, x contains %d, %d, %d\n", a, b, x); } void Add(int a, int b, int &x){ b = b - a; x = x + b; Output Enter two numbers: 6 8 a, b, x contains 6, 2, 102 a, b, x contains 6, 8, 102 CS12 - Computer Programming 1 Chapter 5

Programming Exercise Print the sum and average of two numbers using Pass by value Convert a value in inches to its equivalent value in Feet and inches using Pass by value Chapter 5 CS12 - Computer Programming 1