Vous êtes sur la page 1sur 31

# TA ZC142 Computer Programming Lecture 9 Date: 06/01/2013

Last Lecture
Sorting Techniques Insert an element into a Sorted Array Remove an element from a Sorted Array Enumerated data types Introduction to user defined data types
Structures Union

Todays Agenda
Problem Solving Technique
Top Down Design

Functions

Content
User-Defined Functions
Functions with no arguments and no return value Functions with arguments and no return value Functions with arguments and return value Functions with no arguments but returns a value

## Passing Array as an argument to a function

One dimensional Strings
4

Top-down Design
Design Approach Principle
Divide and Conquer

Steps
Divide the problem into sub-problems Solve the sub-problems Combine the (sub-)solutions

Top-down Design
How do you solve the sub-problem? Divide-and-conquer again! Steps
1. Divide the problem into sub-problems 2. Repeat step 1 for each sub-problem until the problem is small enough to have an atomic solution. 3. Combine the solutions.

User-Defined Functions

Functions
A function is a named, independent section of C code that performs a specific task and optionally returns a value to the calling program/function. A C program consists of one or more function definitions, including exactly one that must be called main().

## Function Declaration and Definition

A declaration merely provides a function prototype.

It includes the return type and the list of parameters. Syntax: return_type function_name( arg-typ name-1,...,arg-typ name-n);
Example: int hex(int val);

Function Parameters
There are two types of function parameters: Formal parameters:
Appear in a declaration or a definition of a function.

Actual parameters:
Appear in a call to the function.

Examples: int f(int x); here x is a formal parameter i = f(3); here 3 is the actual parameter corresponding to the formal parameter.

## Complete C Program Using Function

long int cube(int); /* Function declaration */ int main() Actual parameter { long int c; c = cube(5); /* Function call */ printf(Cube of 5 = %ld,c); return; } /* Function definition starts from here */ long int cube(int num) Formal parameter { long int cb; /*Local variable declaration */ cb = num*num*num; return cb; /* Return value */ }

What will be the output??? int Addnum(int, int); int main() { int a = 10,b = 20; printf("%d %d %d",a,b,Addnum(a,b)); return; } int Addnum(int a, int b) { int c; c = a+b; return c; }

## Functions with no arguments and no return value

void displayline(void); Calling function void sum(void); void main() void sum() { displayline(); {int a,b; sum(); printf(Enter a and b\n); scanf(%d %d,&a,&b); } void displayline() printf(Sum = %d ,(a+b)); } { int j; for(j=0;j<40;j++) printf(*); Called functions }
7/12/2013 14

## Functions with arguments and no return value

void displayline(char); void Sum(float, float); void main() { char ch; float a,b; printf(Enter character to print in line\n); scanf(%c,&ch); displayline(ch); printf(Enter value of a and b\n); scanf(%f %f,&a,&b); Sum(a,b); }
7/12/2013 15

Functions Definition void displayline(char ch) { int j; for(j=0;j<40;j++) printf(%c,ch); } void Sum(float x, float y) { printf(Sum = %f ,(x+y)); }
7/12/2013 16

## Functions with arguments and return value

int Add(int, int); int Sub(int, int); int Div(int, int); /* Function Prototype */ int main() { int x,y; printf(Enter two values:\n); scanf(%d %d,&x,&y); printf(Sum =%d,Add(x,y));/*Function Call*/ printf(Subtraction = %d,Sub(x,y)); printf(Division = %d,Div(x,y)); return; }
7/12/2013 17

Function Definition

int Add(int a,int b) { return(a+b); } int Sub(int a,int b) { return(a-b); } int Div(int a,int b) { return(a/b); }
7/12/2013 18

int fact(int ); Function for computing Factorial of a number int main() { int n=5; printf("factorial of %d = %d",n,fact(n)); } int fact(int n) { int f=1,i=2; while(i<=n) { f = f * i; i++; } return(f); }
7/12/2013 19

No arguments but returns a value int ReadNumber(void); void main() { int num = ReadNumber(); printf(%d,num); } int ReadNumber(void) { int number; scanf(%d,&number); return(number); } Example: getchar()
7/12/2013 20

Compute sine series : x x3/3! + x5/5! - ..n terms Write a function power(x,a) and fact(a).

7/12/2013

21

## Compute the roots of quadratic equation.

ax2 + bx + c = 0
Write a function quad(a,b,c,sign) to find roots of a quadratic equation and call this function in main().

7/12/2013

22

float quad(int a,int b,int c,int sign); int main() { int a,b,c; float root1,root2; int flag=0; while(!flag) { printf("enter a,b,c \n"); scanf("%d %d %d",&a,&b,&c); if((b*b)>(4*a*c) && a != 0) break; else printf("Invalid entries\n"); } root1 = quad(a,b,c,1); root2 = quad(a,b,c,0); printf("root1 = %f root2 = %f\n",root1,root2); }
7/12/2013 23

float quad(int a,int b,int c,int sign) { float disc,r; disc = b*b - 4*a*c; if(sign) { r =(-b + sqrt(disc))/(2*a); return(r); } else { r =(-b - sqrt(disc))/(2*a); return(r); } }
7/12/2013 24

## Passing Arrays To Functions (Pass by reference)

One- dimensional Array: int Maximum(int a[], int size); main() { int val[5]={3,5,2,7,4}; printf(%d\n,Maximum(val,5)); } int Maximum(int a[], int size) { int i, max = a[0]; for(i=0; i<size; i++) if(max<a[i]) max = a[i]; return(max); }
7/12/2013 25

## Rules to pass an Array to a function

1. The function must be called by passing name of

the array.
2. In the function definition, the formal parameter

must be an array type; the size of the array does not need to be specified.
3. The function prototype must show that the

argument is an array.

7/12/2013

26

Write a program using functions to sort elements in an 1D array. Write a function to read an array, print an array and sort an array.

Extend the above program by adding few more functions to search an element in an array, find maximum in an array, find sum of elements in an array and return the sum. return the results found in each function to the calling function.
7/12/2013 27

void readArr(int a[],int size); void printArr(int a[],int size); void sortArr(int a[],int size); int main() { int a[5]; printf("Enter elements in an array\n"); readArr(a,5); printf("Display elements in Array before sorting\n"); printArr(a,5); sortArr(a,5); printf("Display elements in Array after sorting\n"); printArr(a,5); } 7/12/2013

28

Programming Exercise

Write a function to calculate the gross salary of a employee if his basic salary is input through the keyboard. if basic salary is less than Rs.1500 then HRA = Rs. 500 and DA = 90% of basic. If salary is equal or greater than 1500 then HRA = 10% and DA = 95% of basic salary. Basic salary is passed to the function and gs salary is returned.

START

INPUT bs

YES

is bs < 1500

NO

## Hra = bs * 10/100 Da = bs * 95/100

Gs = bs + hra + da PRINT GS

STOP

Any Doubts ?