ETE 132 Lecture 6 By Munirul Haque.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
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 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
Computer Organization and Design Pointers, Arrays and Strings in C
User-Written Functions
Lecture 8 String 1. Concept of strings String and pointers
Functions and Structured Programming
Quiz 11/15/16 – C functions, arrays and strings
Functions Dr. Sajib Datta
Pointers.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Pointers.
INC 161 , CPE 100 Computer Programming
Command-Line Arguments
Arrays in C.
CSI-121 Structured Programming Language Lecture 16 Pointers
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Visit for more Learning Resources
11/10/2018.
CSC 253 Lecture 8.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Inputs Output
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
CSC 253 Lecture 8.
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Functions I Creating a programming with small logical units of code.
Dale Roberts, Lecturer IUPUI
Functions.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
Programming in C Pointer Basics.
Arrays Strings and Parameter Passing CSCI N305
Pointers Call-by-Reference CSCI 230
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
A function with one argument
Programming in C Pointer Basics.
Topics discussed in this section:
Simulating Reference Parameters in C
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
EECE.2160 ECE Application Programming
Exercise Arrays.
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
CS1100 Computational Engineering
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
EECE.2160 ECE Application Programming
Functions I Creating a programming with small logical units of code.
CPS125.
Scope Rules.
C Parameter Passing.
Presentation transcript:

ETE 132 Lecture 6 By Munirul Haque

Topics Functions Parameters Return Types Call by value Call by reference

Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make them easier to think and reason about. Facilitates code re-use (not just within one program, but in others) Makes it possible to hide away details of one task from the rest of program, which does not care.

Functions The ideal function performs a single, well-defined task. Example: Testing if a given number is prime. Computing the number of days in a given month, etc.

Code Reuse The less specific a function is to the program in which it is initially used, the more reusable it tends to be On the other hand, don’t waste lots of time writing a general function if it’s not needed for your task. Code reuse is nice, but atypical in practice.

Anatomy of a function Function has the general form return-type function-name( parameters ) { declarations; statements; }

Example int power( int base, int exp ) { int i, p = 1; for( i = 1; i <= exp; i++ ) p *= base; return p; }

Functions Function definitions can appear in any order. int power( int base, int exp); Names used by power (base, exp, p) are “local” to power, and are not visible to any other functions. DO NOT USE GLOBAL VARIABLES !!!

Return Type If the return-type is non-void, every path of execution must end in a return statement. If the return-type is void, then return; can be used to terminate the function at any point. Break vs Return statement.

Example In Context int power( int base, int exp ); void main() { int i = 3, j = 4, k; k = power (i, j); printf( "%d raised to the power %d is %d.\n", i, j, k); } int power( int base, int exp ) { int i, p = 1; for( i = 1; i <= exp; i++ ) p = p*base; return p;

Prototype, Parameter and Argument int power( int base, int exp ); at the top is a function prototype; tells compiler what kind of function power is, so that it can check function calls Distinction between parameters of a function: variables in parenthesized list (for power, parameters are base, exp), arguments of a function call: the actual values passed to the function (here, 3, 4)

Yet another example void print_square( int a ); int square( int a ); void main() { int i; for( i = 1; i <= 10; i++ ) print_square( i ); } void print_square( int a ) { printf( "The square of %d is: %d\n", a, square(a) ); int square( int a ) { return( a * a );

Call by value In C, all arguments to functions are passed by value: the function is given copies of the arguments, and not the originals A called function is given the values of its arguments in temporary variables, not the originals A called function cannot directly alter a variable in the calling function – it can only alter its private, temporary copy

Call By Value Example void increment( int a ) { a++; } void main() { int b = 3; increment( b ); printf( "%d\n", b ); // prints 3

Call by value: an asset? Here’s one example: int power( int base, int n ) { int i, p; p = 1; for( i = 1; i <= n; i++ ) p = p * base; return( p ); } int power( int base, int n ) { int p; for( p = 1; n > 0; n-- ) p = p * base; return( p ); }

What is wrong here? void get_age( int other_age ); void main() { int age; get_age( age ); printf( "Your age is: %d\n", age ); } void get_age( int other_age ) { printf( "Please enter your age.\n" ); scanf( "%d", &other_age );

Example 1 #include <stdio.h> void print_fruits( int oranges, int apples ); void main() { int apples, oranges; printf( "How many apples do you have?" ); scanf( "%d", &apples ); printf( "How many oranges do you have?" ); scanf( "%d", &oranges ); print_fruits( apples, oranges ); }

Example1 (cont.) void print_fruits( int oranges, int apples ) { printf( "You have %d oranges and %d apples.", oranges, apples ); }

Example 2 #include <stdio.h> int get_age( int year ); void main() { int age, year; year = 0; age = get_age( year ); printf( "Your age is: %d\n", age ); }

Example 2 (cont.) int get_age( int year ) { printf( "What year were you born?" ); scanf( "%d", &year ); return( 2007 - year ); }

Example 3 #include <stdio.h> void do_one_score( int total ); void main() { int total, i; total = 0; for( i = 1; i <= 10; i++ ) do_one_score( total ); printf("Total: %d\n", total); }

Example 3 (cont.) void do_one_score( int total ) { int score; printf( "Enter a score: " ); scanf( "%d", &score ); total += score; }

Example 3 Modified #include <stdio.h> int running_total( void ); void main() { int i, total; for( i = 1; i <= 10; i++ ) total += do_one_score( ); printf("Total: %d\n", total); }

Example 3 Modified (cont.) int running_total( void ) { int score; printf( "Enter a score: " ); scanf( "%d", &score ); return score; }

Call by Reference In C, by call by reference, it is possible to pass address: The address of argument is copied into parameters. A called function can directly alter a variable of the calling function.

Call by Reference void swap (int *x, int *y); void main() { int i, j; i = 10; j = 20; printf(“BEFORE: i=%d j=%d”, i, j); // pass the address of i and j swap (&i, &j); printf(“AFTER: i=%d j=%d”, i, j); } void swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } BEFORE: i=10 j=20 AFTER: i=20 j=10

Calling Function with Arrays void display (int num[ ]) { int i; for (i=0; i<10; i++) printf(“%d ”, num[i]); } void display (int num[ ]); void main() { int t[10], i; for (i=0; i<10; i++) t[i] = i; display (t); /*display (&t[0]); same*/ } void display (int *num) { // alternative way int i; for (i=0; i<10; i++) printf(“%d ”, num[i]); }

Calling Function with String void print_upper (char *str); void main() { char s[80]; gets (s); print_upper (s); } void print_upper (char *str) { int t; for(i=0; str[i]; i++) str[i] = str[i] -32; // *(str+i) -= 32; same puts(str); }

Returning pointers char* strcat (char* str1, char* str2); void main() { char s1[ ] =“Hello_”; char s2[ ] =“world”; char *s3 = strcat(s1, s2); printf(“%s”, s3); } char* strcat (char* str1, char* str2) { int len = strlen(str1); for (i=0; str2[i] != ‘\0’; i++) str1[len+i] = str2[i]; str1[len+i] = ‘\0’; return str1; }