EASTERN MEDITERRANEAN UNIVERSITY EENG212 Algorithms and Data Structures Functions in C.

Slides:



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

1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 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.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
EENG212 Algorithms and Data Structures
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
© 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.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
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.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
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 : Top Down Design with Functions By Suraya Alias.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
1 CSC103: Introduction to Computer and Programming Lecture No 16.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
 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.
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.
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
ECE Application Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
Dr. Shady Yehia Elmashad
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Formatted and Unformatted Input/Output Functions
Dr. Shady Yehia Elmashad
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 6: User-Defined Functions I
EENG212 ALGORITHMS & DATA STRUCTURES
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

EASTERN MEDITERRANEAN UNIVERSITY EENG212 Algorithms and Data Structures Functions in C

OUTLINE ♦ Review of Functions in C ♦ Types of Function Calls ♦ Call by Value ♦ Call by Reference

FUNCTIONS ♦ Modules in C are called functions. A function in C is defined to be the program segment that carries out some specific, well defined task. There are two types of functions:  Library functions  Programmer Defined functions

Library Functions C standard library provides a rich collection of functions for performing I/O operations, mathematical calculations, string manipulation operations etc. For example, sqrt(x) is a function to calculate the square root of a double number provided by the C standard library and included in the header file.

Library Functions Ex: : double a=9.9, b; b = sqrt(a); printf(“The square root of %f is %f”, a, b); : Other functions such as exp(x) (exponential function e x ) and pow(x,y) (x y )... can be used as they are needed. Note that each program in C has a function called main which is used as the root function of calling other library functions.

Programmer Defined Functions In C, the programmers can write their own functions and use them in their programs. Ex: The following program calls the programmer defined function called square to calculate the square of the numbers from 1 to 10.

Programmer Defined Functions #include int square( int); /* function prototype */ int main() { int x; printf(“the squares of numbers from 1 to 10 are:\n”); for(x=1 ;x <= 10; x++) { y = square(x); /*function call */ printf(“the sqare of %d = %d\n”,x, y); } return 0; } /*function definition */ int square (int a) { int b; b = a * a; return b; }

Function Prototype Function prototypes are always declared at the beginning of the program indicating the name of the function, the data type of its arguments which is passed to the function and the data type of the returned value from the function.

Example The following program calculates the average of the 3 float numbers entered by the user. #include float average(float, float, float); /*function prototype */ int main( ) { float a, b, c; printf(“Enter three numbers please\n”); scanf(“%f”,&a); scanf(“%f”,&b); scanf(“%f”,&c); printf(“the average of 3 numbers = %.3f\n”,average(a,b,c)); return 0; }

Example /*function definition */ float average(float x, float y, float z) /*local variables x,y,z */ { float r; r = (x+y+z)/3; return r; }

Example The following program displays all the integers between two integer numbers. #include void printNumbers(int, int); /*function prototype */ int main( ) { int a, b, c; printf(“Enter two integers and I will print the all the numbers in between\n”); scanf(“%d%d”,&n,&m); printNumbers(n,m); /*function call */ }

Example void printNumbers(int x, int y) { int i; /*local variable */ if(x<=y) for(i=x;i<=y;i++) printf(“%d \t”,i); else for(i=y;i<=x;i++) printf(“%d \t”,i); return; /*optional */ }

TYPES OF FUNCTION CALLS Call by Value: When a function is called by an argument/parameter which is not a pointer the copy of the argument is passed to the function. Therefore a possible change on the copy does not change the original value of the argument.

Example Write a program to calculate and print the area and the perimeter of a circle. Note that theradius is to be entered by the user. (Use Call by value approach) # include /*The function calls are Call by Value*/ #define pi 3.14 float area(float); float perimeter(float); int main( ) { float r, a, p; printf(“Enter the radius\n”); scanf(“%f”,&r); a = area(r); p = perimeter(a); printf(“The area = %.2f, \n The Perimeter = %.2f”, a, p); return 0; }

Example float area(float x) { return pi*x*x; } float perimeter(float y) { return 2.0*pi*y; }

TYPES OF FUNCTION CALLS Call by Reference: When a function is called by an argument/parameter which is a pointer (address of the argument) the copy of the address of the argument is passed to the function. Therefore a possible change on the data at the referenced address change the original value of the argument.

Example Write a program to calculate and print the area and the perimeter of a circle. Note that the radius is to be entered by the user. (Use Call by reference approach) #include /*The function calls is Call by Reference*/ #define pi 3.14 void area_perimeter(float, float *, float *); int main( ) { float r, a, p; printf(“Enter the radius\n”); scanf(“%f”,&r); area_perimeter(r,&a,&p); printf(“The area = %.2f, \n The Perimeter = %.2f”, a, p); return 0; }

Example void area_perimeter(float x, float *aptr, float *pptr); { *aptr = pi*x*x; *pptr = 2.0*pi*x; }