FUNCTION CSC128.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6: User-Defined Functions I
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:
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Scis.regis.edu ● CS-361: Control Structures Week 2 Dr. Jesús Borrego Lead Faculty, COS Regis University 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions + Overloading + Scope
Chapter 9: Value-Returning Functions
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Functions, Part 2 of 2 Topics Functions That Return a Value
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
User Defined Functions
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions Imran Rashid CTO at ManiWeber Technologies.
Chapter 10: Void Functions
Eizan Aziz CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Functions, Part 2 of 3 Topics Functions That Return a Value
Standard Version of Starting Out with C++, 4th Edition
CPS125.
Presentation transcript:

FUNCTION CSC128

User – defined Function Contents Predefined Function User – defined Function

Predefined function

Functions that have been defined by the producer of a compiler. Predefined Function Functions that have been defined by the producer of a compiler. Declaration of the functions are stored in header file. (.h extension)

Predefined Function Header files Functions Descriptions math.h pow(x, y) Raise to power. sqrt(x) Compute square root. floor(x) Round down value. ceil(x) Round up value. abs(x) Absolute value string.h strcpy(x,y) Copy sequence of character strcmp(x,y) To compare two sequence of character strcat(x,y) To concatenate sequence of character

Function call: is used to call or to invoke a function. Predefined Function # include<iostream.h> # include<math.h> void main() { int x,y,z; y = 2, z = 4; x = pow(z,y); cout<<“x:”<<x; } Function call: is used to call or to invoke a function. z and y are called parameters or arguments of the function pow. Function call

Variables

Variables Two types of variables: Local variable Global variable

Variables – local variable Variable that is declared within a body of a function definition. Variable that is declared within the main() function are said to be local to main function.

Variables – local variable Example: Local variable #include <iostream.h> void main() { int a; … }

Variables – global variable Variable that is declared outside and above main function. Accessible to all function in the program. It is not wise to use global variables any more than you have to.

Variables – global variable Example: global variable #include <iostream.h> int a; void main() { … }

User – defined function

User-defined function Functions that are defined / written by the programmer / user in a program. Characteristics of user-defined function: A function name is named with unique name. Can be called from other function A function performs a specific task. Task is a discrete job that the program must perform as part of its overall operation. A function is independent. A function may receive values from the calling program (function call). A function may return value to the calling program (function call).

User-defined function In general a function has the following syntax: returnValueType functionName(list of parameters) { … }

User-defined function void/ value returning function. With/ without parameters. returnValueType functionName(list of parameters) { … }

User-defined function To create a user defined function, you must write the following: Function prototype (function’s declaration) Function definition Function call If the function definition is before the main, you do not need the function prototype.

Function prototype Is used to declare a function. Problem statement: to create a function to calculate total of 2 numbers. Steps: Identify function name. void/ value returning function? With/ without parameter?

void function without parameter void function with parameter Function prototype void function without parameter void function with parameter Value returning function, without paramter. Value returning function, with parameter. void calTotal(); void calTotal(int a, int b); int calTotal(); int calTotal(int a, int b);

Is placed in between preprocessor directives and main function. Function prototype Is placed in between preprocessor directives and main function. #include <iostream.h> void calcTotal (); void main() { … }

Function definition Is a block of code (statements) that perform specific task. Statements are usually declaration and/or executable statements.

void function without parameter General syntax:

void function without parameter Example: parameter_list return_type function_name void calTotal() { int a, b, result; a = 1, b = 2; result = a + b; cout<<result; } Function header function_body

void function without parameter Function call: is used to invoke/ to call a function to execute task. Is placed inside main function. Example: void main() { calTotal(); }

void function without parameter Full code: #include<iostream.h> void calTotal(); void main() { calTotal(); } Function prototype Function call Function header void calTotal() { int a, b, result; a = 1, b = 2; result = a + b; cout<<result; } Function definition

void function without parameter Exercise 1: using void function, write a program to calculate an average of 3 numbers.

void function with parameter Parameter is ways how calling function and called function are communicated. Through parameter, data can pass from calling function to called function and from called function to calling function. Formal parameters are optional Two types of parameter: Value parameter Reference parameter

void function with parameter Value parameter A formal parameter that receives a copy of the content of the corresponding actual parameter. Reference parameter* A formal parameter that receives the location of the corresponding parameter.

void function with parameter (value) void calTotal(int x, int y) { int result; result = x + y; cout<<result; } Formal parameter

void function with parameter (value) #include<iostream.h> void calTotal(int, int); void main() { int a, int b; a = 2, b = 3 calTotal(a, b); } void calTotal(int x, int y) { int result; result = x + y; cout<<result; } Actual parameter

void function with parameter (value) Example: void findStatus(int); void main() { int mark; cout<<“enter mark:”; cin>>mark; findStatus(mark); } void findStatus(int value) { if (value>=80) cout<<“excellent”; else if(value>=60) cout<<“good”; else if(value>=50) cout<<“pass”; else cout<<“fail”; }

void function with parameter (value) Exercise 2: using void function with parameter, write a program that require a user to input 2 numbers and determine the maximum number.

Value returning function Is a function that after completing its assigned task, return precisely one value to the calling function (function call). Why we return a value? To use it for further calculation or process. Is used in an assignment statement or in an output statement.

Value returning function Function definition: Return type or data type int calTotal(int x, int y) { int result; result = x + y; return result; }

Value returning function #include<iostream.h> int calTotal(int, int); void main() { int a,b,z; a = 2, b = 3 z = calTotal(a, b); cout<<“total :”<<z; } Function prototype Function call: Assignment statement int calTotal(int x, int y) { int result; result = x + y; return result; }

Value returning function #include<iostream.h> int calTotal(int, int); void main() { int a,b; a = 2, b = 3 cout<<“total :”<< calTotal(a, b); } Function call: Output statement int calTotal(int x, int y) { int result; result = x + y; return result; }

Value returning function Exercise 3: using value returning function, write a program to determine maximum value of two numbers.

Value returning function Exercise 4: using value returning function write a program to calculate an average of three numbers.

Function (reference parameter)

Function – reference parameter If a formal parameter is a reference parameter It receives the address (memory location) of the corresponding actual parameter During program execution to manipulate the data The address stored in the reference parameter directs it to the memory space of the corresponding actual parameter Can pass one or more values from a function Can change the value of the actual parameter

Function – reference parameter A reference parameter receives and stores the address of the corresponding actual parameter Reference parameters are useful in three situations: Returning more than one value Changing the actual parameter When passing the address would save memory space and time.

Function – reference parameter

Function – value parameter #include <iostream.h> void addFour(int a, int b); void main() { int num1,num2; num1 = 3; num2 = 1; cout<< num1<<num2<<endl; addFour(num1,num2); cout<<num1<<num2<endl; } void addFour(int a, int b) { a = a + 4; b++; cout<<a<<b<<endl; } Value parameter In main BEFORE calling addFour function In main AFTER calling addFour function In addFour function num1 3 a 7 num1 3 num2 1 b 2 num2 1

Function – reference parameter #include <iostream.h> void addFour(int& a, int b); void main() { int num1,num2; num1 = 3; num2 = 1; cout<< num1<<num2<<endl; addFour(num1,num2); cout<<num1<<num2<endl; } void addFour(int &a, int b) { a = a + 4; //line 1 b++; //line 2 cout<<a<<b<<endl; } Reference parameter When the value of a changes, value num1 changes as well Reference parameter In main BEFORE calling addFour function In main AFTER calling addFour function In addFour function num1 3 a 7 num1 7 num2 1 b 2 num2 1

Function – reference parameter How it works? In main BEFORE calling addFour function In addFour function num1 3 a num2 1 b 1 Before statement in line 1 execute!!

In main BEFORE calling addFour function In addFour function num1 7 a num2 1 b 2 In function addFour, after statement in line 1 and line 2 execute!!

Scope of a local and global variable #include <iostream.h> void calTotal(); void main() { int a; … } void calTotal() { int a; } Variable a in function main() is different from variable a in Function calTotal()

Global variable #include <iostream.h> void calTotal(); int num1; void main() { int num2; total = num1 + num2 … } void calTotal() { int num2; total = num1 – num2; } Variable num1 is a global variable that are accessible to all function definitions in the file.

Summary Void function: a function that does not have a data type A return statement without any value can be used in a void function to exit function early The heading of a void function starts with the word void To call a void function, you use the function name together with the actual parameters in a stand-alone statement

Summary (continued) Two types of formal parameters: value parameters and reference parameters A value parameter receives a copy of its corresponding actual parameter A reference parameter receives the address (memory location) of its corresponding actual parameter

Summary (continued) If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter Variables declared within a function (or block) are called local variables Variables declared outside of every function definition (and block) are called global variables

Summary (continued) If a formal parameter needs to change the value of an actual parameter, in the function heading you must declare this formal parameter as a reference parameter Variables declared within a function (or block) are called local variables Variables declared outside of every function definition (and block) are called global variables

Thank You !