Functions. Example u int max(int a, int b); int main(){ int x; x = max(5,8); x = max(x,7); } int max(int a, int b){ return a>b?a:b; }

Slides:



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

Spring Semester 2013 Lecture 5
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
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.
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
 2007 Pearson Education, Inc. All rights reserved C Functions.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
 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.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© 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.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
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.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
Learners Support Publications Functions in C++
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
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,
© 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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
CSCI 171 Presentation 6 Functions and Variable Scope.
Review of Lectures 12, 13, 14 -Functions -Arrays -Strings.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
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.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Functions Course conducted by: Md.Raihan ul Masood
-Neelima Singh PGT(CS) KV Sec-3 Rohini
C Language By Sra Sontisirikit
Module 4 Functions – function definition and function prototype.
Deitel- C:How to Program (5ed)
Functions in C Mrs. Chitra M. Gaikwad.
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions I Creating a programming with small logical units of code.
Chapter 6 - Functions Outline 5.1 Introduction
Functions and Modular Programming
In C Programming Language
Introduction to Problem Solving and Programming
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

Functions

Example u int max(int a, int b); int main(){ int x; x = max(5,8); x = max(x,7); } int max(int a, int b){ return a>b?a:b; }

What is a C Function? u A function receives zero or more parameters, performs a specific task, and returns zero or one value. u A function is invoked by its name and parameters. –No two functions have the same name in your C program. –No two functions have the same name AND parameter types in your C++ program. –The communication between the function and invoker is through the parameters and the return value. u A function is independent: –It is “completely” self-contained. –It can be called at any places of your code and can be ported to another program. u Functions make programs reusable and readable.

Syntax u Function Prototype: return_type function_name (type1 name1, type2 name2,..., typen namen); u Function Definition: return_type function_name (type1 name1, type2 name2,...,typen namen) {....statements... } Function header The parameters

Some Examples u Function Prototype Examples double squared (double number); void print_report (int); int get _menu_choice (void); u Function Definition Examples double squared (double number) { return (number * number); } void print_report (int report_number) { if (report_nmber == 1) printf(“Printer Report 1”); else printf(“Not printing Report 1”); } return type void means it returns nothing void parameter list means it takes no parameters

Passing Arguments u Arguments are passed as in Java and Pascal u Function call: func1 (a, b, c); u Function header int func1 (int x, int y, int z) –Each argument can be any valid C expression that has a value: –For example: x = func1(x+1,func1(2,3,4),5); u Parameters x y z are initialized by the value of a b c u Type conversions may occur if types do not match.

Parameters are Passed by Value u All parameters are passed by value!! –This means they are basically local variables initialized to the values that the function is called with. –They can be modified as you wish but these modifications will not be seen in the calling routine! #include int twice(int x) { x=x+x; return x; } int main() { int x=10,y; y=twice(x); printf("%d,%d\n",x,y); }

Returning a Value u To return a value from a C function you must explicitly return it with a return statement. u Syntax: return ; –The expression can be any valid C expression that resolves to the type defined in the function header. –Type conversion may occur if type does not match. –Multiple return statements can be used within a single function (eg: inside an “if-then-else” statement…)

Local Variables u Local Variables int func1 (int y) { int a, b = 10; float rate; double cost = 12.55; } u Those variables declared “within” the function are considered “local variables”. u They can only be used inside the function they were declared in, and not elsewhere.

A Simple Example #include int x=1; /* global variable - bad! */ void demo(void); int main() { int y=2; /* local variable to main */ printf ("\nBefore calling demo(), x = %d and y = %d.",x,y); demo(); printf ("\nAfter calling demo(), x = %d and y = %d.\n",x,y); return 0; } void demo () { int x = 88, y =99; /* local variables to demo */ printf ("\nWithin demo(), x = %d and y = %d.",x,y); }

Placement of Functions u For large programs –Manage related functions in a.c file –Write a.h file containing all the prototypes of the functions –#include the header file in the files that uses the functions. u For small programs, use the following order in the only one file: –All prototypes –main() function –Other functions u mymath.h int min(int x,int y); int max(int x,int y); u mymath.c int min(int x,int y) { return x>y?y:x; } int max(int x,int y) { return x>y?x:y; }

Recursion - An Example unsigned int factorial(unsigned int a); int main () { unsigned int f,x; printf("Enter value between 1 & 8: "); scanf("%d", &x); if (x > 8 || x < 1) printf (”Illegal input!\n"); else { f = factorial(x); printf ("%u factorial equals %u\n", x,f); } unsigned int factorial (unsigned int a) { if (a==1) return 1; else { a *= factorial(a-1); return a; }