You are on page 1of 12

C Programming – Lecture V

Instructor Özgür ZEYDAN

Exam Question
You are going to write a C program that
rolls two dice.
Dice are created by random number
function (from 1 to 6) in C programming.
If the sum of two dices is equals to 7 then
computer will display a message of “you
win” otherwise display a message of “you

\n").d2). randomize(). } . /* d1:dice1 d2:dice2 */ printf("Lucky 7 dice game. else printf("You lose!").d2.h> #include<stdlib.h> #include<conio.d1.h> main(){ int d1. return(0). d1=1+rand()%6. getch().\nPress a key to roll dices.Pseudocode Start Initialize d1 and d2 Display (Lucky 7 dice game press a key to roll dices) Read key Reset random function Generate number for d1 Generate number for d2 Display (You roll d1-value and d2-value) If d1+d2 is equals to 7 Display (You win!) otherwise Display (You lose!) Read key Stop C code #include<stdio. printf("You roll %d and %d\n". d2=1+rand()%6. getch(). if (d1+d2==7) printf("You win!").

In modular programming the various tasks that your overall program must accomplish are assigned to individual functions and the main program basically calls these functions in a certain order. identified by the keyword main. Just add more functions to extend program capability. Each piece more manageable than the original program Easy to modify and expand. Software reusability. Use existing functions as building blocks for new programs . Easy to debug. Make that code block a function and call it when needed. Reasons for Modular Programming Construct a program from smaller pieces or components Avoids code repetition.Modular Programming A function in C is a small “sub-program” that performs a particular task. and enclosed by left and right braces is a function. returns to the operating system. The main body of a C program. and when terminated. It is called by the operating system when the program is loaded. Don’t have to repeat the same block of code many times in your code. and supports the concept of modular programming design techniques.

Function Definitions Function definition format: return-value-type function-name (parameter-list) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int) void . If something returned: return expression. declares parameters (default int) Function Definitions return-value-type function-name (parameter-list) { declarations and statements } Declarations and statements: function body Variables can be declared inside function body Function can not be defined inside another function Returning control If nothing returned: return. .function returns nothing Parameter-list: comma separated list.

} .Function Prototypes Used to validate functions Prototype only needed if function definition comes after use in program Examples: int factor(int). void intro(void). /* function prototype */ /* function prototype */ Functions Functions that do not receive or send data. You can use: void function-name (void) { Statements.

h> void intro(void). /* function prototype */ int main(void) /* main function */ { intro(). } void intro(void) /* intro function */ { printf("======================\n").Example – 1 Write a C code to display an introduction message (given below) on the beginning of the program. /* function call */ statements. } . printf("* Programmer : Özgür ZEYDAN *\n"). Use function (function will neither receive nor return any value) Introduction message: ====================== * Programmer : Özgür ZEYDAN * ====================== Example – 1 #include<stdio. printf("======================\n\n").h> #include<conio.

h> int factor(int). Example – 2 (using local variables) #include<stdio. /* function call */ getch(). At this time. scanf("%d". } . Then computer calculates factorial of a given integer.Example – 2 Write a C program that asks user to write an integer values. /* local variable */ printf("Enter integer value to calculate its factorial: ").h> #include<conio. return(0). printf("Factorial of %d is %d". /* function prototype */ int main(void) /* main function */ { int x.x.&x). use modular programming! Define a function that receives an integer from main function. calculates and returns value of its factorial.factor(x)).

/* function prototype */ int main(void) /* main function */ { printf("Enter integer value to calculate its factorial: "). /* global variable */ int factor(int). } . printf("Factorial of %d is %d". /* function call */ getch(). return fac.&x). scanf("%d".i.h> int x.Example – 2 (using local variables) int factor( int a) { int fac=1. } Example – 2 (using global variables) #include<stdio.x.i++) fac*=i.i<=a.factor(x)). /* local variables */ for (i=1.h> #include<conio. return(0).

return fac. Use a function two make factorial calculations. /* local variables */ for (i=1.Example – 2 (using global variables) int factor( int x) { int fac=1.i<=x.i.i++) fac*=i. } Example – 3 Write a C program that calculates Combination of a given two numbers `m` and `n`. m!  m  =   n   n ! (m − n )! .

/* function call */ Example – 3 getch(). printf("Enter m and n values: "). /* function prototype */ int main(void) /* main function */ { int m. return(0). printf("Combination of (%d/%d) is %d".&m. } int factor( int x) { int fac=1.i++) fac*=i.&n).\n").h> #include <conio. } .factor(m)/(factor(n)*factor(m-n))).m.n. printf("Combination (m/n) calculator. return fac.Example – 3 #include <stdio.i<=x.h> int factor(int).n.i. /* local variables */ for (i=1. scanf("%d %d".

the recursive function will call itself repeatly until the runtime stack overflows. C supports creating recursive function with ease and efficient. Or in a recursive way: N! = 1 if N <=1 and N*(N-1)! if N > 1 . All you know in mathematics the factorial of a positive integer N is defined as follows: N! = N*(N-1)*(N-2)…2*1.C Recursive Function Recursive function is a function that contains a call to itself. Recursive function must have at least one exit condition that can be satisfied. Otherwise. Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily. Example of Using Recursive Function Recursive function is closely related to definitions of functions in mathematics so we can solving factorial problems using recursive function.

1).h> int factor(int a) { if(a <= 1) return 1.Example – 4 # include<stdio.x.factor(x)). return a * factor (a . … } . } void main() { … printf(“Factorial of %d is %d".