Why functions segments of code that repeat several times

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Structures Spring 2013Programming and Data Structure1.
Functions a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value a sub-program when we.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays in C. What is Array? The variables we have used so far can store a single value. Array is a new type of variable capable of storing many values.
Stack and Heap Memory Stack resident variables include:
User Interaction and Variables
CSC215 Lecture Advanced Pointers.
UNIT 5 C Pointers.
Functions and Pointers
Lesson One – Creating a thread
Pointers, Enum, and Structures
C Programming Tutorial – Part I
2016.
Chapter 2 Overview of C.
Quiz 11/15/16 – C functions, arrays and strings
CS1010 Programming Methodology
Functions Dr. Sajib Datta
Array 9/8/2018.
C Short Overview Lembit Jürimägi.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Programmazione I a.a. 2017/2018.
Pointers.
Functions and Pointers
CSCI206 - Computer Organization & Programming
Classes and Objects.
Structures vol2.
Qsort.
7 Arrays.
Functions.
CS150 Introduction to Computer Science 1
Pointers Pointers point to memory locations
CS150 Introduction to Computer Science 1
In C Programming Language
Arrays Arrays A few types Structures of related data items
POINTER CONCEPT 4/15/2019.
Submitted By : Veenu Saini Lecturer (IT)
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Chapter 9: Pointers and String
POINTER CONCEPT 8/3/2019.
Presentation transcript:

Why functions segments of code that repeat several times better readability abstraction of code smaller program size IAG0581

Structural programming int main(void){ int a, b, c; do{ printf("Please enter A: "); scanf("%d", &a); if(a <= 0) printf("A must be positive!\n"); }while(a <= 0); printf("Please enter B: "): scanf("%d", &b); if(b <= 0) printf("B must be positive!\n"); }while(b <= 0); //... return 0; } IAG0581

Functional programming int readPositiveInt(char *name){ int x; do{ printf("Please enter %s: ", name); scanf("%d", &x); if(x <= 0) printf("%s must be positive!\n", name); }while(x <= 0); return x; } int main(void){ int a, b, c; a = readPositiveInt("A"); b = readPositiveInt("B"); //... return 0; IAG0581

Declaring a function returntype functionName(parameters){ functionBody } parameters is just a list of variables separated by comma: int a, int b use "void" if no parameters IAG0581

Datatypes, simple types char a single character or small number: int integer number float floating point number void signifies no type When used as parameters, these are unidirectional IAG0581

Datatypes, pointers char * pointer to a character or character array int * pointer to integer or integer array float * pointer to float or floating point array void * pointer to memory without type specified When used as parameters, these are bidirectional IAG0581

Datatypes, arrays char[] a character array int[] an array of integers float[] a array of floating point numbers When used as parameters, these are bidirectional IAG0581

Calling a function even if no parameters, ( ) are necessary even if function returns something it can be called without assignment For example: int doSmth(void){ //... return 1; } int main(void){ doSmth(); return 0; IAG0581

C compiler single pass compiler accepts only variables and functions that have been declared before they are used sometimes necessary to declare that a function exists before providing code for that function IAG0581

Function prototypes used to tell compiler that a function by that name exists and what parameter types it takes parameter names are unimportant For example: int readPositiveInt(char *); IAG0581

Function prototypes int readPositiveInt(char *); int main(void){ //... a = readPositiveInt("A"); } int readPositiveInt(char *name){ IAG0581

Variable scope variable exists in block where it is declared outside block it doesn't exist variable can be redefined in an inner block each function is separate block IAG0581

Variable scope int fn1(void){ int x = 2; //... } int main(void){ int a, b, x; x = 5; a = fn1(); if(a < b){ float x = 3.0; x++; IAG0581

Function with no data exchange void welcome(void){ printf("This program ..."); printf("Written by..."); } int main(void){ welcome(); ... IAG0581

Getting a result from a function result is returned with return keyword. what's returned should be of same type as declared returntype, if not it will be converted IAG0581

Getting a result from a function int test(void){ float f = 0.8; return f; // will return 0, cos converted to int } int test2(void){ ... return; //warning !! void test3(void){ int x = 10; return x; //warning !! IAG0581

Getting multiple results only 1 item of returntype can be returned returntype could be complex type, like record or pointer etc. use pointers as parameters so function can access memory outside of its scope Example you should know: scanf("%d", &a); & operator returns address of where variable is located in memory IAG0581

Getting multiple results int div(int a, int b, int *result, int *remainder){ if(b == 0) return 0; *result = a / b; *remainder = a % b; return 1; } int main(void){ //... int s, t; div(15, 6, &s, &t); IAG0581

Getting multiple results with arrays arrays are always pointers to first element function can access all elements if it knows how many there are IAG0581

Getting multiple results with arrays void doSmth(int n, float M[n]){ int i; for(i = 0; i < n; i++) M[i] = //... } int main(void){ int Arr[10]; doSmth(10, Arr); ... IAG0581

Conditions a function call can be put anywhere in code, also in place of conditions if you have complex condition then make it into a function, especially if needed more than once For example: while( a < b && a < c || a + b < c + 1 ){ //... } IAG0581

Conditions int isTrue(int a, int b, int c){ return a < b && a < c || a + b < c + 1; } int main(void){ //... if(!isTrue(3, b, x)) while(isTrue(3, b, x)){ IAG0581

Function as parameter permits you call a function that has been passed as a parameter to your code alternatively you can write a function that behaves as a parameter for some function you want to call typical example qsort void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*)); IAG0581

Function qsort() qsort() sorts an array if you supply it a function that can compare 2 elements in it, essentially a condition function int isLess(const void *aa, const void *bb){ int *a = (int*)aa; int *b = (int*)bb; return (*a) - (*b); } int main(void){ int M[10]; //... qsort(M, 10, sizeof(int), isLess); IAG0581

Function as parameter int readInt(char *name, int (*suits)(int), char *cond){ int x; do{ printf("Please enter %s: ", name); scanf("%d", &x); if(!suits(x)) printf("%s must be %s!\n", name, cond); }while(!suits(x)); return x; } int isPos(int a){ return a > 0; int main(void){ int a, b, c; a = readInt("A", isPos, "positive"); b = readInt("B", isPos, "positive"); //... IAG0581