You are on page 1of 26

# Intro to Pogramming

Functions
• a group of declarations and statements that is assigned a name
• effectively, a named statement block • usually has a value

• a sub-program
• when we write our program we always define a function named main • inside main we can call other functions
• which can themselves use other functions, and so on…

This is a function defined outside main printf("square of %g is %g\n".Example . return 0. } Here is where we call the function square . scanf("%lf".&num). } int main(void) { double num.Square #include <stdio.num. printf("enter a number\n").h> double square(double a) { return a*a. square(num)).

Why use functions? • they can break your problem down into smaller sub-tasks • easier to solve complex problems • they make a program much easier to read and maintain • abstraction – we don’t have to know how a function is implemented to use it • generalize a repeated set of instructions • we don’t have to keep writing the same thing over and over .

…) { function body.Characteristics of Functions return-type name(arg_type1 arg_name1. return value. arg_type2 arg_name2. } int main(void) { … } . } double square(double a) { return a*a.

Return Statement • Return causes the execution of the function to terminate and usually returns a value to the calling function • The type of the value returned must be the same as the return-type defined for the function (or a ‘lower’ type) • If no value is to be returned. the return-type of the function should be set to ‘void’ .

&num).h> int factorial (int n) { int i. printf("enter a number\n"). i++) fact *= i. printf("%d!=%d\n". } .Factorials galore #include <stdio. return fact. scanf("%d". fact = 1. i<=n. for (i=2.num. } return 0.factorial(num)). int main(void) { int num.

A Detailed Example Write a program that receives a numerator(nominator) and a denominator from the user. and displays the reduced form of the number. . the program should display 2/3. For example. if the input is 6 and 9.

} . &n. n. d/gcd).h> int main(void) { int n. return 0. printf("Please enter nominator and denominator: ").Example – solution (step I) #include <stdio. &d). d. Calculate n’s and d’s Greatest Common Divisor printf("The reduced form of %d/%d is %d/%d". d. scanf("%d%d". n/gcd.

&n.h> int main(void) { int n. n/g. &d). d. printf("Please enter nominator and denominator: "). return 0. scanf("%d%d". g.Example – solution (step II) #include <stdio. } . d. n. d/g). printf("The reduced form of %d/%d is %d/%d". d). g = gcd(n.

x) then the gcd of y and x equals the gcd of x and r. Assume both are positive. } . while(x > 0) { tmp = x. } return y. The function uses the fact that the if r = mod(y. y = tmp.Example – solution (step III) /* Returns the greatest common divisor of its two parameters. int y) { int tmp. */ int gcd(int x. x = y % x.

printf("Please enter … : "). d/g). n/g. d. d. g. d). g = gcd(n. &n.GCD – step by step int main(void) { int n. n 6 9 d g --- } . scanf("%d%d". return 0. printf("The reduced form…". n. &d).

d. g = gcd(n.GCD – step by step int main(void) { int n. &d). &n. d. return 0. printf("Please enter … : "). d/g). d). n/g. n. scanf("%d%d". n 6 9 d g --- } . g. printf("The reduced form…".

} . int y) { int tmp.GCD – step by step int gcd(int x. 6 x 9 y tmp --- while(x > 0) { tmp = x. } return y. x = y % x. y = tmp.

} return y. } .GCD – step by step int gcd(int x. y = tmp. x = y % x. int y) { int tmp. 6 x 9 y tmp --- while(x > 0) { tmp = x.

x = y % x. } return y.GCD – step by step int gcd(int x. y = tmp. } . int y) { int tmp. 6 x 9 y tmp 6 while(x > 0) { tmp = x.

GCD – step by step int gcd(int x. } . } return y. 3 x 9 y tmp 6 while(x > 0) { tmp = x. x = y % x. y = tmp. int y) { int tmp.

} return y.GCD – step by step int gcd(int x. y = tmp. int y) { int tmp. x = y % x. while(x > 0) { tmp = x. } x 3 6 y tmp 6 .

} return y. x = y % x.GCD – step by step int gcd(int x. } . int y) { int tmp. y = tmp. 3 x 6 y tmp 6 while(x > 0) { tmp = x.

GCD – step by step int gcd(int x. 3 x 6 y tmp 3 while(x > 0) { tmp = x. x = y % x. } . int y) { int tmp. y = tmp. } return y.

0 x 6 y tmp 3 while(x > 0) { tmp = x. } . int y) { int tmp. y = tmp. x = y % x. } return y.GCD – step by step int gcd(int x.

y = tmp. } return y. x = y % x.GCD – step by step int gcd(int x. } x 0 3 y tmp 3 . int y) { int tmp. while(x > 0) { tmp = x.

GCD – step by step int gcd(int x. } return y. } . x = y % x. x 0 3 y tmp 3 while(x > 0) { tmp = x. int y) { int tmp. y = tmp.

x = y % x.GCD – step by step int gcd(int x. y = tmp. } x 0 3 y tmp 3 . } return y. while(x > 0) { tmp = x. int y) { int tmp.

d. d. g = gcd(n. &n. scanf("%d%d". d/g). n/g. d). return 0. n. printf("The reduced form…". g.GCD – step by step int main(void) { int n. &d). printf("Please enter … : "). n 6 9 d 3 g } .

0 otherwise) .Exercise Write a program that gets a positive integer from the user and prints all the prime numbers from 2 up to that integer. (Use a function that returns 1 if its parameter is prime.