function (3)

function (3) - Functions and Structured Programming 1...

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

View Full Document Right Arrow Icon
1 Functions and Structured Programming
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. The construction of a program should embody top- down design of the overall problem into finite subtasks. Each subtask can be coded directly as a function . These functions can be used in main to solve the overall problem.
Background image of page 2
3 Functions Functions are the building blocks of C and the place where all program activity occurs. Function topics include : Function types Function variables Function arguments Function scopes Return values Function prototypes Recursion
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 General syntax : ret-type function-name ( parameter list ) { body of the function return (expression) } A function may return any type of data except an array. The parameters receive the values of the arguments when the function is called.
Background image of page 4
5 VOID used to indicate: A. - No Return Value . B. - No Parameters or Arguments . * This allows Type compatibility to be handled by the compiler. void Fname ( void )
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 A variable is a named location in memory that is used to hold a value that can be modified by the program. All variables must be declared before they can be used. Variables
Background image of page 6
7 Where variables are declared : - Inside functions : local variables - Outside of all functions : global variables - In the function heading : formal parameters
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Local Variable Declared inside a function . Can be used only by statements that are inside the block in which the variables are declared. A local variable is created upon entry into its block and destroyed upon exit. void func1(void) { int x ; x = 10; } void func2(void) { int x ; x = -199; }
Background image of page 8
9 void f(void) { int t; scanf("%d", &t); if(t==l) { char s[80]; /* created only upon entry into this block */ } /* s not known here */ }
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 #include <stdio.h> int main (void) { int x ; x = 10; if(x == 10) { int x ; /* this masks the outer x */ x = 99; printf("Inner x: %d\n", x); 99 } printf("Outer x: %d\n", x); 10 return 0; }
Background image of page 10
11 Local variables are allocated in the Run-time stack . #include <stdio.h>
Background image of page 11

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

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

Page1 / 38

function (3) - Functions and Structured Programming 1...

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

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