Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name.

Slides:



Advertisements
Similar presentations
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
Advertisements

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Exercise 5.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
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,
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Unary Not operator !  !true = false  !false = true.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
C++ CODES PART#04 1 COPY ENGR.SABA MUGHAL FROM COMPUTER SYSTEMS DEPARTMENT.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
User Defined Functions
Fucntions in C++ Malik Jahan Khan
-Neelima Singh PGT(CS) KV Sec-3 Rohini
LESSON 2 Basic of C++.
Computer Skills2 for Scientific Colleges
Functions and an Introduction to Recursion
Variables A piece of memory set aside to store data
Programming fundamentals 2 Chapter 1:Array
Chapter 5 Functions.
Chapter 2.2 Control Structures (Iteration)
C++ Arrays.
לולאות קרן כליף.
Programming fundamentals 2 Chapter 2:Pointer
CS 1430: Programming in C++.
Programming fundamentals 2 Chapter 2:Function
CSC1201: Programming Language 2
פונקציות לעיתים קרובות לא נוח להגדיר את כל התוכנה בתוך גוף אחד.
User Defined Functions
Compound Assignment Operators in C++
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Conditional Construct
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.
Review for Final Exam.
CS150 Introduction to Computer Science 1
Chapter 2.2 Control Structures (Iteration)
Selection Control Structure
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Review for Final Exam.
Local, Global Variables, and Scope
The Function Prototype
Functions and an Introduction to Recursion
CS150 Introduction to Computer Science 1
Fundamental Programming
CSC1201: Programming Language 2
Programming fundamentals 2 Chapter 1: Functions (Sub-Algorithms)
Repetition Statements (Loops) - 2
Functions Imran Rashid CTO at ManiWeber Technologies.
CS1201: Programming Language 2
HNDIT11034 More Operators.
Pointers.
Programming fundamentals 2 Chapter 3:Pointer
Functions Chapter No. 5.
C++ PROGRAMMING SKILLS Part 3 User-Defined Functions
Presentation transcript:

Function User defined function is a code segment (block) that perform an specific action and may return a value. Function Definition: Return_DT F_name ( list of formal parameters) { Action_ body ; } If Return_DT is void Then: Action_body doesn’t contain return statement. Else: Action_body contains return statement. Calling (invoking) Function: F_name (Actual parameters);

Example #include <iostream.h> int Max (int Value1, int Value2) { if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl; Function Definition Calling the function in an expression like cout<<, condition, assignment statements

Example #include <iostream.h> int Max (int Value1, int Value2) { if (Value1 > Value2) return Value1; else return Value2; } void main() { int a, b; cout<<"\nPlease Enter the values of a and b: "; cin>>a>>b; cout<<"\n the maximum one is: "<<Max(a,b)<<endl;

Example #include <iostream.h> int Sum (int A, int B) { return (A+B); } void main() { int N1, N2, S; cout<<"\n Please Enter N1 and N2: "; cin>>N1>>N2; S = Sum(N1,N2); cout<<"\nSum= "<<S<<endl;

Example #include <iostream.h> bool Positive (int Num) { if (Num > 0) return true; else return false; } void main() { int Number; cout<<"\nEnter Number: "; cin>> Number; if (Positive(Number)) cout<<"\n the number is positive"; cout<<"\n the number is negative"; cout<<endl;

Example #include <iostream.h> float Area (int R) { return (3.14 * R * R ); } void main() { int Radius; cout<<"Enter the Redius: "; cin>>Radius; cout<<"\nCircle Area is: "<<Area(Radius); cout<<endl;

Example #include <iostream.h> long Power(int Base, int Exp) { int M=1; for(int i=1; i<=Exp; i++) M*=Base; return M; } void main() { int B, E; cout<<"\nEnter Base: "; cin>>B; cout<<"\nEnter Exponent: "; cin>>E; cout<<"\n Result= "<<Power(B,E); cout<<endl;

Example #include <iostream.h> long Fact (int Num) { int F = 1, i = Num; while (i>=1){ F *= i; i--; } return F; } void main() { int Number; cout<<"Enter an integer number: "; cin>>Number; cout<<endl<<Number<<"!= "<<Fact(Number); cout<<endl;

Void Returned Data Type #include <iostream.h> void Print(char Ch, int n) { for (int i=1; i<=n; i++) cout<<Ch; cout<<endl; } void main() { char Sym; int Number; cout<<"\nEnter the Symbol: "; cin>>Sym; cout<<"\nHow many times: "; cin>>Number; Print(Sym,Number); No Return Statement

Example #include <iostream.h> int Mul(int V1, int V2) { return V1 * V2; } void Result() { cout<<"\n5 x 9 = "<<Mul(5,9); cout<<"\n4 x 7 = "<<Mul(4,7); cout<<"\n6 x 4 = "<<Mul(6,4)<<endl; } void main() { Result() ; }

Function Prototype #include <iostream.h> int Mul(int, int); int Add(int, int); void Show(); void main() { Show(); } int Mul(int X, int Y) { return X*Y; } int Add(int X, int Y) { return X+Y; } void Show() { int A=10, B=20; cout<<Add(A,B)<<'\t'<<Mul(A,B)<<endl; } Function Prototype contains only data types But may contain identifiers.

Call by value When calling, the value of actual parameter will be copied to the formal parameter. #include <iostream.h> void Increment(int); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int X) { ++X; }

Call By reference When calling, the reference formal parameter will be an alternative name to the actual parameter. #include <iostream.h> void Increment(int&); void main() { int A = 10; Increment(A); cout<<A<<endl; } void Increment(int &X) { ++X; }

Call By reference When calling, the pointer formal parameter will points to the actual parameter. #include <iostream.h> void Increment(int*); void main() { int A = 10; Increment(&A); cout<<A<<endl; } void Increment(int *X) { ++*X; }

Recursion Function call itself #include <iostream.h> int Fact (int N) { if (N<=1) return 1; else return N * Fact(N-1); } void main() { cout<<Fact(5)<<endl;

Example #include <iostream.h> int Zap (int N) { int Z; if (N<=1) Z=1; else Z= Zap(N-1) + Zap(N-3); return Z; } void main() { cout<<Zap(5)<<endl;

Array as parameter -Array name is pointer (call by reference) #include <iostream.h> void Increment (int a[]) { for (int i=0; i<5; i++) a[i] += 10; } void main() { int b[5] = {10,20,30,40,50}; Increment(b); for(int i=0; i<5; i++) cout<<b[i]<<'\t'; cout<<endl; }

Array as parameter -Array element (call by value) #include <iostream.h> void Increment (int a) { ++a; } void main() { int b[5] = {10,20,30,40,50}; Increment(b[1]); cout<<b[1]<<endl; }