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

52
Algorithms and Programming Functions Lecture 28

Transcript of Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for...

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

Algorithms and Programming Functions

Lecture 28

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

Summary of Previous Lecture

while statement for statement break statement Nested loops

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

Summary of C program

execution steps

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

We are using Turbo C for

editing source code

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

Our Source code is passed to the compiler

which checks Errors and translate your source

code into machine code

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

Object Code is formed after

compilation, which is computer

understandable code!

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

Linker Links Object Code formed with

the other Library Files..

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

Library Files path settings which you already configured!

All the header files including stdio.h are

placed here!

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

After Linking, executable file is formed , Loader

loads it into memory for further

processes…

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

Today’s Topics Algorithms

FunctionsUsing functions in top-down design

Functions in C LanguageUser defined FunctionsParametersReturn values

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

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

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

Function Definition

Define a function as:

<functionname>

{

<sequence>

}

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

Example: Inviting Saad to a party

Function DefinitionInviteToParty { dial 9876-5432 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() }

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

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

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

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

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

Function Definition

Define a function with parameters as:

<functionname> ( <parameter>,<parameter>, ... ) { <sequence> }

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

Example: Inviting someone to a party

Function Definition

inviteToParty ( person, date, place){ ringUp(person) askToParty(date, place) sayGoodbye(person, date) }

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

Example: Inviting someone to a partyFunction Definition

ringUp( person ){ set number to lookUpNumber(person) dial(number) say "hello," say person say "it's" sayMyName() }

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

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 }

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

Example: Inviting someone to a party

Function Definition

sayGoodbye ( person, date ) { say "Great! See you then. Bye" say person hangUp() }

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

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

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

Build simple functions first Then use them as building blocks for more

complex functions Example: Juggling

Top Down Design and Functions

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

Top-Down and Bottom-Up Design are really two sides of the same coin

Example: Getting a Degree - Top-DownYou don’t just walk in and pick up the piece of

paperYou do first year, second year, third year

(maybe fourth year), then hopefully you can get your degree!

Top Down Design and Functions

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

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

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

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

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

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

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

Functions in C Language Topics

FunctionsParametersReturn values

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

printf() printf and scanf are functions, declared in

the file stdio.hThe preprocessor directive (#) directs the

compiler that the function which you are looking for is declared in the stdio.h file.

#include<stdio.h>

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

User-Defined Functions Create your own functions, similar to

printf() or scanf() Recall a procedure in an algorithm - a

named collection of instructionsInviteToPartyRingUpMakeToParty

A function implements the procedure or function parts of an algorithm.

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

Writing User-defined Functions Need to specify:

the name of the function its parameterswhat it returnsblock of statements to be carried out when the

function is called The block of statements is called the

“function body”

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

Prints a simple greeting.

procedure sayHello{ output “Hello World!”}

Main Program{

do procedure sayHello

}

Example: hello1.c

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

#include <stdio.h>

/* * 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

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

Example: hello1.c

Function definition

Function call

#include <stdio.h>

/* * 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;}

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

Example: hello1.c

Function name

Function body

#include <stdio.h>

/* * 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;}

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

Example: hello1.c

Return type

Formal Parameter List

#include <stdio.h>

/* * 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;}

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

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.

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

/* 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)

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

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); }}

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

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

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

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.

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

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);}

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

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);}

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

Example: badswap.c

Output: 3 5

5 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);}

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

Example: badswap.c

Output: 3 5

5 3

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);}

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

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);}

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

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:

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

Return Values Values are returned by copying a value

specified after the return keyword

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

/* 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

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

/* 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.

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

/* 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.

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

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:

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

Summary We have studied,

What is a Function?How to write algorithm for a function?C Programming functions

Parameters Return types Function Call