Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
1 CSE1301 Computer Programming Lecture 12 Algorithm Design.
Chapter 6: User-Defined Functions I
1 CSE1301 Computer Programming Lecture 12: Algorithm Design.
CSE1301 Computer Programming: Lecture 16 Algorithm Design: Components.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Computer Science 210 Computer Organization Introduction to C.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Programming With C.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
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)
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
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, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Introduction to C Topics Compilation Using the gcc Compiler
CSCE 206 Structured Programming in C
Programming what is C++
User-Written Functions
Chapter 6: User-Defined Functions I
Computer Science 210 Computer Organization
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
CSI 121 Structured Programming Language Lecture 12: Algorithm Design
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
11/10/2018.
Computer Science 210 Computer Organization
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
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
Creating your first C program
Programming Fundamentals Lecture #3 Overview of Computer Programming
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Computing Lecture 08: Functions (Part I)
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

Algorithms and Programming Functions Lecture 28

Summary of Previous Lecture while statement for statement break statement Nested loops

Summary of C program execution steps

We are using Turbo C for editing source code

Our Source code is passed to the compiler which checks Errors and translate your source code into machine code

Object Code is formed after compilation, which is computer understandable code!

Linker Links Object Code formed with the other Library Files..

Library Files path settings which you already configured! All the header files including stdio.h are placed here!

After Linking, executable file is formed, Loader loads it into memory for further processes…

Today’s Topics Algorithms  Functions  Using functions in top-down design Functions in C Language  User defined Functions  Parameters  Return values

Functions A named sequence of instructions Also known as: modules, procedures, subroutines,... Give a name to a standard, frequently used sequence of actions Specify ("call") that sequence by name

Function Definition Define a function as: { }

Example: Inviting Saad to a party Function Definition InviteToParty { dial say "Hello Saad, it's " sayMyName() say "Would you like to come to my party on 10 July?" say "It's at 1 Ravi Road." say "Great! See you then. Bye Saad" hangUp() }

Function Parameters Functions may have parameters They specify variations from call to call So that the same function can do different things Depending on the value of the parameters

Function Parameters … For example:  √4 = 2  √36 = 6 Both the above can be thought of as “calls” to the square root function But one returns 2, the other returns 6 Depending on the value of the parameter

Function Definition Define a function with parameters as: (,,... ) { }

Example: Inviting someone to a party Function Definition inviteToParty ( person, date, place) { ringUp(person) askToParty(date, place) sayGoodbye(person, date) }

Example: Inviting someone to a party Function Definition ringUp( person ) { set number to lookUpNumber(person) dial(number) say "hello," say person say "it's" sayMyName() }

Example: Inviting someone to a party Function Definition askToParty(date, location) { say " Would you like to come to my party on " say date say "It's at" say location }

Example: Inviting someone to a party Function Definition sayGoodbye ( person, date ) { say "Great! See you then. Bye" say person hangUp() }

Top Down Design and Functions Bottom Up Design  Determine what simple sequences you will need to solve the problem  Code those sequences from primitives  Build more complex sequences using the simpler ones as "pseudo-primitives"  Continue building increasingly complex sequences  Stop when you have a sequence which solves the entire problem

Build simple functions first Then use them as building blocks for more complex functions Example: Juggling Top Down Design and Functions

Top-Down and Bottom-Up Design are really two sides of the same coin Example: Getting a Degree - Top-Down  You don’t just walk in and pick up the piece of paper  You do first year, second year, third year (maybe fourth year), then hopefully you can get your degree! Top Down Design and Functions

But how do you do first year?  First semester, second semester How do you do first semester?  4 different subjects... How do you do a subject?... Top Down Design and Functions

Getting a degree - Bottom-Up Do an interesting-looking subject Do another... Keep doing them until you have enough for a degree... Top Down Design and Functions

Obviously you need direction Bottom-up degree-getting strategies might never see you with the right combination of subjects to actually graduate! So elements of Top-down guidance are needed - what are we aiming at? Top Down Design and Functions

Functions in C Language Topics  Functions  Parameters  Return values

printf() printf and scanf are functions, declared in the file stdio.h  The preprocessor directive (#) directs the compiler that the function which you are looking for is declared in the stdio.h file. #include

User-Defined Functions Create your own functions, similar to printf() or scanf() Recall a procedure in an algorithm - a named collection of instructions  InviteToParty  RingUp  MakeToParty A function implements the procedure or function parts of an algorithm.

Writing User-defined Functions Need to specify:  the name of the function  its parameters  what it returns  block of statements to be carried out when the function is called The block of statements is called the “function body”

Prints a simple greeting. procedure sayHello { output “Hello World!” } Main Program { do procedure sayHello } Example: hello1.c

#include /* * Print a simple greeting. */ void sayHello ( void ) { printf( “ Hello World!\n ” ); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; } Prints a simple greeting. procedure sayHello { output “Hello World!” } Main Program { do procedure sayHello } Example: hello1.c

Function definition Function call #include /* * Print a simple greeting. */ void sayHello ( void ) { printf( “ Hello World!\n ” ); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; }

Example: hello1.c Function name Function body #include /* * Print a simple greeting. */ void sayHello ( void ) { printf( “ Hello World!\n ” ); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; }

Example: hello1.c Return type Formal Parameter List #include /* * Print a simple greeting. */ void sayHello ( void ) { printf( “ Hello World!\n ” ); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; }

Parameters Information passed to a function “Formal” parameters are local variables declared in the function declaration. “Actual” parameters are values passed to the function when it is called.

/* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Example: badsort.c Parameters (aka Arguments)

Example: badsort.c /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); }

int main(void) { int x = 3, y = 5; badSort ( 10, 9 ); badSort ( y, x+4 ); return 0; } Example: badsort.c /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Formal parameters Actual parameters

Parameters (cont.) Parameters are passed by copying the value of the actual parameters to the formal parameters. Changes to formal parameters do not affect the value of the actual parameters.

int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } Example: badswap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

Example: badswap.c Output: 3 5 int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

Example: badswap.c Output: int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

Example: badswap.c Output: int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

Example: badswap.c Calling function’s environment: a: 3 b: 5 Called function’s environment: a: 5 b: 3 int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

Parameters (cont.) If a function does not take parameters, declare its formal argument list void. void sayHello ( void ) { printf( “ Hello World!\n ” ); } sayHello(); Function call: Declaration:

Return Values Values are returned by copying a value specified after the return keyword

/* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c Return type

/* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c For example: The value of the expression max(7,5) is the integer 7.

/* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c This style okay.

Return Values (cont.) If a function does not return a value, declare its return type void. void sayHello ( void ) { printf( “ Hello World!\n ” ); } sayHello(); Function call: Declaration:

Summary We have studied,  What is a Function?  How to write algorithm for a function?  C Programming functions Parameters Return types Function Call