Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout<< "Enter number of day(1-7):

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

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.
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.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
GE 211 Function in C++ Dr. Ahmed Telba Math Library Functions Defining a Function: The general form of a C++ function definition is as follows:
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
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.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
1 Chapter 6 - Functions Outline 6.1Introduction 6.2Program Components in C++ 6.6Math Library Functions 6.4Functions 6.5Function Definitions 6.6Function.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© 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.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Chapter 3: User-Defined Functions I
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 12.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
School of EECS, Peking University
FUNCTIONS IN C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
This technique is Called “Divide and Conquer”.
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Functions Najah Alsubaie Kingdom of Saudi Arabia
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
Chapter 6: User-Defined Functions I
C++ PROGRAMMING SKILLS Part 3 User-Defined Functions
Presentation transcript:

Functions in C Programming Dr. Ahmed Telba

If else // if #include using namespace std; int main() { unsigned short dnum ; cout<< "Enter number of day(1-7): "; cin>> dnum; cout<< "\n"; if(dnum == 1) cout << "the day is Friday"; else if(dnum == 2) cout << "the day is Saturday"; else if(dnum == 3) cout << "the day is Sunday"; else if(dnum == 4) cout << "the day is Monday"; else if(dnum == 5) cout << "the day is Tuesday"; else if(dnum == 6) cout << "the day is Tuesday"; else if(dnum == 7) cout << "the day is Thursday"; else cout << "Sorry we're closed "; cout<<'\n'; system("pause"); return 0; }

Switch case --- // switch #include using namespace std; int main() { unsigned short dnum ; cout<< "Enter number of day(1-7): "; cin>> dnum; cout<< "\n"; switch(dnum){ case 1: // if dnum = 1 cout << "the day is Friday"; break; case 2: // if dnum = 2 cout << "the day is Saturday"; break; case 3: // if dnum = 3 cout << "the day is Sunday"; break; case 4: // if dnum = 4 cout << "the day is Monday"; break; case 5: // if dnum = 5 cout << "the day is Tuesday"; break; case 6: // if dnum = 6 cout << "the day is Wednesday"; break; case 7: // if dnum = 7 cout << "the day is Thursday"; break; default: // if dnum 7 cout << "Sorry we're closed "; break; } cout<< "\n"; system("pause"); return 0; }

Type of Variables Global Variables – Variables declared before main() function. – Global variables are defined to all parts in the program – If Global variable is changed in any part in the program, this change appears in all the program Local Variables – Variables defined inside different functions, including main() function – Local variables are defined only in their functions Note: DO NOT use a local variable of the same name as a global variable

Function

Example #include int I; // I is a global variable main() { int K; /* K is a local varaible defined in the main */ }

Functions

Return and Input Types

Function Declarations

Functions (I) Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us in C++. A function is a group of statements that is executed when it is called from some point of the program. The following is its format: type name ( parameter1, parameter2,...) { statements } where: type is the data type specified of the data returned by the function. name is the identifier by which it will be possible to call the function. parameters (as many as needed): Each parameter consists of a data type specified followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas. statements is the function's body. It is a block of statements surrounded by braces { }.

Example #include void print2number(int,int); main() { int x,y; x=3; y=4; print2number(x,y); } void print2number(int number1,int number2) { cout<<”number1,number2,number3; }

function example #include using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int main () { int z; z = addition (5,3); cout << "The result is " << z; return 0; } The result is 8

Math Library Functions Revisited

Introduction Function Definition Void function Global Vs Local variables Random Number Generator Recursion Function Overloading Sample Code C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Functions in C++ Experience has shown that the best way to develop and maintain large programs is to construct it from smaller pieces(Modules) This technique Called “Divide and Conquer” main() { Return 0; } Bad Development Approach Easer To Design Build Debug Extend Modify Understand Reuse Better Organization Wise Development Approach main() { } function f1() { --- } function f2() { --- }

Functions in C++(Cont.) In FORTRAN Modules Known as Subprograms In Pascal Modules known as Procedures & Functions In C++ Modules Known as Functions & Classes Programs use new and “prepackaged” modules – New: programmer-defined functions and classes – Prepackaged: from the standard library

About Functions in C++ Functions invoked by a function–call-statement which consist of it’s name and information it needs (arguments) Boss To Worker Analogy  A Boss (the calling/caller function) asks a worker (the called function) to perform a task and return result when it is done. Main Boss Function AFunction B Function Z Worker Function B2Function B1 Worker Note: usual main( ) Calls other functions, but other functions can call each other

Function Calling Function Arguments can be: -Constant sqrt(9); -Variable sqrt(x); -Expression sqrt( x*9 + y) ; sqrt( sqrt(x) ) ; Functions called by writing functionName (argument); or functionName(argument1, argument2, …); Example cout << sqrt( ); sqrt (square root) function The preceding statement would print 30 All functions in math library return a double

Function Calling cout<< sqrt(9); Function Name argument 3 Output Parentheses used to enclose argument (s) Calling/invoking a function –sqrt(x); –Parentheses an operator used to call function Pass argument x Function gets its own copy of arguments –After finished, passes back result

Functions – Modularize a program – Software reusability Call function multiple times Local variables – Known only in the function in which they are defined – All variables declared in function definitions are local variables Parameters – Local variables passed to function when called – Provide outside information

Function prototype – Tells compiler argument type and return type of function – int square( int ); Function takes an int and returns an int – Explained in more detail later 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 Function Definition

Syntax format for function definition returned-value-type function-name (parameter-list) { Declarations of local variables and Statements } – Parameter list Comma separated list of arguments – Data type needed for each argument If no arguments, use void or leave blank – Return-value-type Data type of result returned (use void if nothing returned)

Function Definition Example function int square( int y ) { return y * y; } return keyword – Returns data, and control goes to function’s caller If no data to return, use return; – Function ends when reaches right brace Control goes to caller Functions cannot be defined inside other functions

#include using namespace std; // Creating and using a programmer-defined function. int square( int ); // function prototype int main() { // loop 10 times and calculate and output // square of x each time for ( int x = 1; x <= 10; x++ ) cout << square( x ) << " "; // function call cout << endl; system("pause"); return 0; // indicates successful termination } // end main // square function definition returns square of an integer int square( int y ) // y is a copy of argument to function { return y * y; // returns square of y as an int } // end function square

#include using namespace std; int square(int); // prototype int cube(int); // prototype main() {int i; for (int i=1;i<=10;i++){ cout<< i<< "square=" << square(i) << endl; cout<< i<< "cube= " <<cube(i) << endl; } // end for system("pause"); return 0; } // end main function int square(int y) //function definition { return y*y; // returned Result } int cube(int y) //function definition { return y*y*y; // returned Result }

#include using namespace std; double maximum( double, double, double ); // function prototype int main() { double number1, number2; double number3; cout << "Enter three real numbers: "; cin >> number1 >> number2 >> number3; // number1, number2 and number3 are arguments to the maximum function call cout << "Maximum is: " << maximum( number1, number2, number3 ) << endl; system("pause"); return 0; } double maximum( double x, double y, double z ) { double max = x; // assume x is largest if ( y > max ) // if y is larger, max = y; // assign y to max if ( z > max ) // if z is larger, max = z; // assign z to max return max; // max is largest value } // end function maximum

Add two number #include using namespace std; void add2Nums(int,int); main() { int a, b; cout<<"enter tow Number:"; cin >>a >> b; add2Nums(a, b); system("pause"); return 0; } void add2Nums(int x, int y) { cout<< x<< "+" << y << "=" << x+y; }

function example #include using namespace std; int subtraction (int a, int b) { int r; r=a-b; return (r); } int main () { int x=5, y=3, z; z = subtraction (7,2); cout << "The first result is " << z << '\n'; cout << "The second result is " << subtraction (7,2) << '\n'; cout << "The third result is " << subtraction (x,y) << '\n'; z= 4 + subtraction (x,y); cout << "The fourth result is " << z << '\n'; return 0; }

#include using namespace std; void printmessage () { cout << "I'm a function!"; } int main () { printmessage (); return 0; }

x=2, y=6, z=14 /// passing parameters by reference #include using namespace std; void duplicate (int& a, int& b, int& c) { a*=2; b*=2; c*=2; } int main () { int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; return 0; }

// more than one returning value #include using namespace std; void prevnext (int x, int& prev, int& next) { prev = x-1; next = x+1; } int main () { int x=100, y, z; prevnext (x, y, z); cout << "Previous=" << y << ", Next=" << z; return 0; } Previous=99, Next=101

// default values in functions #include using namespace std; int divide (int a, int b=2) { int r; r=a/b; return (r); } int main () { cout << divide (12); cout << endl; cout << divide (20,4); return 0; } 6 5

#include using namespace std; long factorial (long a) { if (a > 1) return (a * factorial (a-1)); else return (1); } int main () { long number; cout << "Please type a number: "; cin >> number; cout << number << "! = " << factorial (number); return 0; } Please type a number: 9 9! =

// declaring functions prototypes #include using namespace std; void odd (int a); void even (int a); int main () { int i; do { cout << "Type a number (0 to exit): "; cin >> i; odd (i); } while (i!=0); return 0; } void odd (int a) { if ((a%2)!=0) cout << "Number is odd.\n"; else even (a); } void even (int a) { if ((a%2)==0) cout << "Number is even.\n"; else odd (a); } Type a number (0 to exit): 9 Number is odd. Type a number (0 to exit): 6 Number is even. Type a number (0 to exit): 1030 Number is even. Type a number (0 to exit): 0 Nu

Example

Including Functions

Variable Scope

Example: Bessel Function Γ(n) = (n − 1)!

Example: Bessel Function doubleJ(int n,double xx) { doublexasymp=17.5; double tk,s; int NN=60,k,i; if(xx==0. && n==0) return 1.; if(xx<xasymp) { tk=pow((xx/2.),n)/fact(n); s=tk; for(k=0;k<=NN;k++) { tk=-tk*xx*xx/4./(k+1.)/(k+n+1.); s=s+tk; } else s=sqrt(2./Pi/xx)*cos(xx-Pi/4.-n*Pi/2.); return(s); }