Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

Spring Semester 2013 Lecture 5
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
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:
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
CPS120: Introduction to Computer Science Functions.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
Arrays in C.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Introduction to C Programming CE Lecture 6 Functions, Parameters and Arguments.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Why functions segments of code that repeat several times
Functions and Pointers
Hassan Khosravi / Geoffrey Tien
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions and Structured Programming
Functions Dr. Sajib Datta
9. FUNCTIONS.
Functions Dr. Sajib Datta
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Functions and Pointers
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
A function with one argument
EECE.2160 ECE Application Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
ETE 132 Lecture 6 By Munirul Haque.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Presentation transcript:

Functions Dr. Sajib Datta

Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions can cause an action ▫Such as printf() and scanf() and others can return a value for your program to use ▫Such as strlen() and strcmp()

Why use functions Make reuse of code easier Reduce number of places to update Hide implementation, letting programmer focus on functionality

Why use functions cont. You want to write a program that does the following: Read in a list of numbers Sort the numbers Find their average Print out their average ▫#include ▫int main(void) ▫{ ▫ int list[10]; ▫ readlist(list, 10); ▫ sortlist(list, 10); ▫ average(list, 10); ▫ printavg(list, 10); ▫ return 0; ▫}

What do you need to know about functions? How to define functions properly? How to call them up for use? How to set up communication between functions?

Three terms A function definition ▫Specifies exactly what the function does A function call ▫Causes the function to be executed Function prototype ▫Tells the compiler what sort of function your_function() is

An example of a function #include void printstars(int num); /*Function prototype*/ int main(void) { int numstar; do { printf("Please input an integer (>0):"); scanf("%d", &numstar); }while(numstar <= 0); printstars(numstar);/*A function call*/ return 0; } void printstars(int num) /*A function definition */ { int i; for (i=0; i< num; i++) { printf("*"); } printf("\n"); }

Function definitions When defining our own functions, the general form is ▫return_type function_name(input_type variable_name) ▫{ ▫/* something happens here */ ▫} Example: ▫int addnumbers(int number1, int number2) ▫{ ▫int sum = number1 + number2; ▫return sum; ▫}

Function definitions cont. The names of the formal parameters in the function definition do not need to match the names in the function call, but the number and types should match. To return a value, we use the return keyword. We can declare variables in our function just as we did in main. We can call other functions from within our function.

Function definitions cont. The types of variables that we can pass or receive from a function can be any of the types that we declare variables to be–int, float, array (actually, we pass the address of the array), etc. What type do we use if we are not passing or not returning anything? Void Example: ▫void print2numbers(int number1, int number2) ▫{ ▫printf("%d + %d is %d\n", number1, number2, ▫number1 + number2); ▫}

Function definitions cont. When a function is called, an execution environment is created for the function and control passes to its code. The execution environment contains memory for the parameters to the function and any variables defined in the function. When the function terminates execution, the memory occupied by the function will be returned to the system for other use. So the parameters stored there will not be guaranteed to retain the contents.

Function definitions cont. We need to know the following when using variables in functions: The process used in this lecture for providing variable values to our function is called pass by value. When doing so, a copy of the variable is provided. Variables declared outside the function are unknown to the function unless we pass them. Variables declared within a function block are known only to that function.

Passing by value in function calls #include int f1(int y); int main(void) { int x = 15; printf("In main, before calling f1(x), x is %d.\n", x); f1(x); printf("In main, after calling f1(x), x is %d.\n", x); return 0; } int f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); return y; }

Wrong! Warning or error #include int main(void) { int x = 15; f1(x); return 0; } void f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); } Order of function definition

We must let the compiler know about the function prior to using it by either: Placing the function definition before main Placing a function declaration (or prototype) before main and define the function later Proper guideline

You have to define a function before you use it. Or you can declare the function before you use it and give the definition later. #include void f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); } int main(void) { int x = 15; f1(x); return 0; } Writing definition before main()

#include void f1(int y); int main(void) { int x = 15; f1(x); return 0; } void f1(int y) { y = y + 1; printf("In f1, y = %d\n", y); } Writing function prototype/declaration before main()

//Compare two integers #include int LargerInt(int a, int b); int main(void) { int x, y; printf("Please input two integers:"); scanf("%d", &x); scanf("%d", &y); if(LargerInt(x,y)) { printf("a is larger than b.\n"); } else { printf("a is not larger than b.\n"); } return 0; } int LargerInt(int a, int b) { if(a > b) return 1; else return 0; }

#include #define SIZE 10 int sum(int ar[], int n); int main(void) { int myarr[SIZE] = {20, 10, 5, 39, 4, 16, 19, 26, 31, 20}, answer; answer = sum(myarr, SIZE); printf("The total sum of the integers in myarr is %d.\n", answer); printf("The size of myarr is %d bytes.\n", sizeof(myarr)); return 0; } int sum(int ar[], int n) { int i, total = 0; for(i = 0; i<n; i++) total += ar[i]; return total; } Function and array