Return function Random function Recursion function Function in C 1.

Slides:



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

Recursion Prog #include <stdio.h> #include<conio.h> main()
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Programming Recursion “To Iterate is Human, to Recurse, Divine” -- L. Peter Deutsch.
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
Guidelines for working with Microsoft Visual Studio.Net.
Guidelines for working with Microsoft Visual Studio 6.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
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.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Computer Programming for Engineers. Outline Tic-Tac-Toe (O-X Game) Drawing 3x3 grid Receiving the inputs Checking for a winner Taking turns between.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
FUNCTIONS A function is a set of instructions that can perform a specific task accordingly. A function is a program that performs a specific task according.
Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument.
Chinese Proverb says……... Advantages void main( ) { int n, k, i ; printf(“\n Enter number:-”); scanf(“%d”, &n); for(i=2 ; i
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Computer Programming for Engineers
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture 2. Algorithms and Algorithm Convention 1.
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
CSE 220 – C Programming Pointers.
C Functions -Continue…-.
Module 4 Functions – function definition and function prototype.
Functions in C Mrs. Chitra M. Gaikwad.
Chapter 5 - Functions Outline 5.1 Introduction
Looping.
Chapter 6 - Functions Outline 5.1 Introduction
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
A function with one argument
Relational, Logical, and Equality Operators
In C Programming Language
1-6 Midterm Review.
Main() { int fact; fact = Factorial(4); } main fact.
Presentation transcript:

Return function Random function Recursion function Function in C 1

Syntax Return_type fun_name( arguments) { scope of function; return value; } 2

Example Write a program by using function to add two numbers and return the result. #include int SUM( ) { int a,b; a=2; b=4; return( a+b); } void main( ) { int S; S = SUM( ); printf (“ \n sum = %d ”, SUM( ) ); printf (“ \n sum = %d ”, S); } Local variable for SUM( ) Calling return function 3

Example #include int SUM( ) ; void main( ) { int S; S = SUM( ); printf (“ \n sum = %d ”, SUM( ) ); printf (“ \n sum = %d ”, S); } int SUM( ) { int a,b; a=2; b=4; return( a+b); } 4

Example #include int SUM( int a, int b) { return( a+b); } void main( ) { int S, x, y; S = SUM(4, 5 ); printf (“ \n sum = %d ”, S); x = 6; y = 7; printf (“ \n sum = %d ”, SUM( x, y) ); } 5

Example #include int SUM( int a, int b); void main( ) { int S, x, y; S = SUM(4, 5 ); printf (“ \n sum = %d ”, S); x = 6; y = 7; printf (“ \n sum = %d ”, SUM( x, y) ); } int SUM( int a, int b) { return( a+b); } 6

Example #include int maximum( int x, int y, int z); void main( ) { int n1, n2, n3; scanf (“ %d %d %d”, &n1, &n2, &n3); printf (“ \n max = %d ”, maximum( n1, n2, n3) ); } int maximum( int x, int y, int z) { int max = x; // assume x is the maximum value if (y > max) {max = y;} if (z > max) {max = z;} return max; } 7

Rand () function The declaration of rand function is found in rand () is used to generate random numbers between 0 and You can call it by assignment (i.e. int i = rand ();) Or in any equation ( x = rand() * 2;) Or print the value of rand () ( printf (“r= %d”, rand());) 8

Example 9 #include void main( ) { int freq1=0; int freq2=0; int freq3=0; int freq4=0; int freq5=0; int freq6=0; int r, face; for (r=1; r<= 6000; r++) { face = 1+ rand() %6; switch (face) { case 1: freq1++; break; case 2: freq2++; break; case 3: freq3++; break; case 4: freq4++; break; case 5: freq5++; break; case 6: freq6++; break; } // end switch }// end for printf (“ \n frequency1 = %d”, freq1); printf (“ \n frequency2 = %d”, freq2); printf (“ \n frequency3 = %d”, freq3); printf (“ \n frequency4 = %d”, freq4); printf (“ \n frequency5 = %d”, freq5); printf (“ \n frequency6 = %d”, freq6); } //end main

Recursion function 10 A recursive function is a function that calls itself either directly or in directly through another function. The recursion step executes while the original call to the function is still open. For example( n! ) You can written like this: int fact = 1; for( i = number; i>=1; i--) fact= fact * i;

Continue 11 But you can written in a recursive function like this: #include long factorial (long number); void main() { int i; for( i = 0; i <= 10; i++) printf(“ \n %d! = %d”, i, factorial(i)); } long factorial (long number) { if (number <= 1) return 1; else return (number* factorial(number-1) ); } long factorial (long number) { if (number <= 1) return 1; else return (number* factorial(number-1) ); } Direct recursion

Continue 12 #include int Sqr ( int x) { return (x*x); } int fun (int z) { return (z + Sqr(3)); } void main ( ) { int a; printf(“ \n a = “); scanf(“%d”, &a); printf(“\n resullt = %d”, fun(a)); } Indirect recursion

Ass 13