algorithms and programming functions lecture 28. summary of previous lecture while statement for...

Post on 17-Jan-2016

235 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

FunctionsUsing functions in top-down design

Functions in C LanguageUser defined FunctionsParametersReturn 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:

<functionname>

{

<sequence>

}

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

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:

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

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 partyFunction 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-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

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

FunctionsParametersReturn values

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>

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.

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”

Prints a simple greeting.

procedure sayHello{ output “Hello World!”}

Main Program{

do procedure sayHello

}

Example: hello1.c

#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

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

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

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

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

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

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

top related