Practice5 - Reminder - Loops int a=1; while(a) while(!a);...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Reminder - Loops int a=1; while(a) while(!a); printf(Got here?); Reminder - Loops #include<stdio.h> int main() { int i,j,flag=0; for (i=0 ; i<10 && flag ; i++) { flag=0; for (j=i+1 ;j<10 ; j++) if (j+i<2) { printf("*"); flag=1; } } return 0; } 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 Example - Square #include <stdio.h> double square( double a) { return a*a; } int main( void ) { double num; printf("enter a number\n"); scanf("%lf",&num); printf("square of %g is %g\n",num,square(num)); return 0; } This is a function defined outside main Here is where we call the function square 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 dont have to know how a function is implemented to use it generalize a repeated set of instructions we dont have to keep writing the same thing over and over Characteristics of Functions return-type name( arg_type1 arg_name1 , arg_type2 arg_name2 , ) { function body; return value; } double square( double a) { return a*a; } int main( void ) { } 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 Factorials galore #include <stdio.h> int factorial( int n) { int i, fact = 1; for (i=2; i<=n; i++) fact *= i; return fact; } int main( void ) { int num; printf("enter a number\n"); scanf("%d",&num); printf("%d!=%d\n",num,factorial(num)); return 0; } A Detailed Example Write a program that receives a nominator and a denominator from the user, and displays the reduced form of the number. For example, if the input is 6 and 9, the program should display 2/3. Example solution (step I) # include <stdio.h> int main( void ) { int n, d; printf("Please enter nominator and denominator: "); scanf("%d%d", &n, &d); Calculate ns and ds Greatest Common Divisor printf("The reduced form of %d/%d is %d/%d", n, d, n/ gcd , d/ gcd ); return 0; }...
View Full Document

Page1 / 56

Practice5 - Reminder - Loops int a=1; while(a) while(!a);...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online