62
C Fundamentals C Fundamentals MELJUN CORTES MELJUN CORTES computer program user Word excel photoshop C Programming C Programming Language Language

MELJUN CORTES C++ chapter 1 c++ fundamentals

Embed Size (px)

Citation preview

C FundamentalsC Fundamentals

MELJUN CORTESMELJUN CORTEScomputer program user

Word

excel

photoshop

C Programming C Programming LanguageLanguage

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 2

IntroductionIntroduction

We will learnWe will learn The C programming languageThe C programming language Structured programming and proper programming techniquesStructured programming and proper programming techniques

This book also coversThis book also covers C FundamentalsC Fundamentals Control StatementsControl Statements Data types, variables and ExpressionsData types, variables and Expressions Arrays and StringArrays and String

This course is appropriate forThis course is appropriate for Technically oriented people with little or no programming Technically oriented people with little or no programming

experience experience Experienced programmers who want a deep and rigorous Experienced programmers who want a deep and rigorous

treatment of the languagetreatment of the language

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

ProgramProgram

Computer = hardware + softwareComputer = hardware + softwareProgram makes computer to general purposeProgram makes computer to general purpose

School of Computer InformationSchool of Computer Information 3

computer program user

Word

excel

photoshop

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

What is computer?What is computer?

Computer processing data by instruction.Computer processing data by instruction.

School of Computer InformationSchool of Computer Information 4

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

Smart PhoneSmart Phone

Smart phone is a kind of computerSmart phone is a kind of computer

School of Computer InformationSchool of Computer Information 5

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

History of computerHistory of computer

Eniac : vacuum tubeEniac : vacuum tube

School of Computer InformationSchool of Computer Information 6

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

Von Neumann Architecture

Store data and program in Main memoryStore data and program in Main memory

School of Computer InformationSchool of Computer Information 7

data

program

Main memory

CPU

Von Neumann

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

CompilerCompiler

We need compilerWe need compiler

School of Computer InformationSchool of Computer Information 9

ProgrammingLanguage

MachineLanguage

Compiler

01000110101010100101010101010110010

111111

if( x > 0 ) pos++;else neg++;

English Korean

Translator

시작이 좋으면 끝도 좋다 ....

A good beginning makes a good ending....

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 10

History of CHistory of C

C C Evolved by Ritchie from two previous programming languages, Evolved by Ritchie from two previous programming languages,

BCPL and BBCPL and B Used to develop UNIXUsed to develop UNIX Used to write modern operating systemsUsed to write modern operating systems Hardware independent (portable)Hardware independent (portable) By late 1970's C had evolved to "Traditional C"By late 1970's C had evolved to "Traditional C"

StandardizationStandardization Many slight variations of C existed, and were incompatibleMany slight variations of C existed, and were incompatible Committee formed to create a "unambiguous, machine-Committee formed to create a "unambiguous, machine-

independent" definitionindependent" definition Standard created in 1989, updated in 1999Standard created in 1989, updated in 1999

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

Father of C languageFather of C language

Dennis Ritchie awarded National Medal of Dennis Ritchie awarded National Medal of TechnologyTechnology

School of Computer InformationSchool of Computer Information 11

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 12

Feature of CFeature of C

System programming available as Low level type high level System programming available as Low level type high level language language ➡ middle level language➡ middle level language                     Low level language : handle to real instructions for computerLow level language : handle to real instructions for computer’’s execution s execution

(machine language of assembly type)(machine language of assembly type)Ex) Assembly languageEx) Assembly language

High level language : support of several control structures and I/O High level language : support of several control structures and I/O instruction (facilitate programming)instruction (facilitate programming)

Ex) Pascal, Delphi, VB, JavaEx) Pascal, Delphi, VB, Java

C programs consist of pieces/modules called functionsC programs consist of pieces/modules called functions Suit structural programmingSuit structural programming

Sequence/Conditional/Repetable structureSequence/Conditional/Repetable structure

Various data structure, operation and function libraryVarious data structure, operation and function library Higher portability to other systemsHigher portability to other systems

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 13

The C Standard LibraryThe C Standard Library

C programs consist of pieces/modules called functionsC programs consist of pieces/modules called functions A programmer can create his own functionsA programmer can create his own functions

Advantage: the programmer knows exactly how it worksAdvantage: the programmer knows exactly how it works Disadvantage: time consumingDisadvantage: time consuming

Programmers will often use the C library functionsProgrammers will often use the C library functions Use these as building blocksUse these as building blocks

Avoid re-inventing the wheelAvoid re-inventing the wheel If a premade function exists, generally best to use it rather than If a premade function exists, generally best to use it rather than

write your ownwrite your own Library functions carefully written, efficient, and portableLibrary functions carefully written, efficient, and portable

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 14

Basics of a Typical C Program Basics of a Typical C Program Development EnvironmentDevelopment Environment

• Phases of C Programs:

1. Edit

2. Preprocess

3. Compile

4. Link

5. Load

6. Execute

Program is created inthe editor and storedon disk.

Preprocessor programprocesses the code.

Loader puts program in memory.

CPU takes eachinstruction and executes it, possibly storing new data values as the program executes.

Compiler creates object code and storesit on disk.

Linker links the objectcode with the libraries

Loader

Primary Memory

Compiler

Editor

Preprocessor

Linker

Primary Memory

.

.

.

.

.

.

.

.

.

.

.

.

Disk

Disk

Disk

CPU

Disk

Disk

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Run Microsoft Visual C++Run Microsoft Visual C++

School of Computer InformationSchool of Computer Information 15

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Select File and NewSelect File and New

School of Computer InformationSchool of Computer Information 16

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Win32 Console ApplicationWin32 Console Application

School of Computer InformationSchool of Computer Information 17

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Empty projectEmpty projectFinishFinishOKOK

School of Computer InformationSchool of Computer Information 18

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Empty projectEmpty project

School of Computer InformationSchool of Computer Information 19

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

File-New-C++source FileFile-New-C++source File Input file nameInput file name

School of Computer InformationSchool of Computer Information 20

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Input this codeInput this code

School of Computer InformationSchool of Computer Information 21

01 # include <stdio.h>0203 int main()04 {05 printf(“%d”, 100-50);06 }

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Build-Build first.exeBuild-Build first.exe

School of Computer InformationSchool of Computer Information 22

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

If an error : missing semicolonIf an error : missing semicolon

School of Computer InformationSchool of Computer Information 23

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

First ProgramFirst Program

Build-Execute First.exeBuild-Execute First.exe

School of Computer InformationSchool of Computer Information 24

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 25

1.1. Understand the components Understand the components of a C programof a C program

All C programs consist of one or more functionsAll C programs consist of one or more functions Each function contains one or more statementsEach function contains one or more statements The general form of a C FunctionThe general form of a C Function

ret-typeret-type     function-name() function-name() ➡ name of the function➡ name of the function

    {{                                                                                 ➡ start of the function➡ start of the function

          statement sequencestatement sequence         ➡ statements of the function➡ statements of the function

       }}                                                                               ➡ end of the function➡ end of the function

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 26

1.1. Understand the components Understand the components of a C programof a C program

Function nameFunction name upper- and lowercase letters of alphabetupper- and lowercase letters of alphabet( A~Z, a~z )( A~Z, a~z ) digitsdigits( 0~9 ) ( 0~9 ) cannot start a function name cannot start a function name underscoreunderscore( _ )( _ ) Case-sensitive Case-sensitive ex) Myfun ex) Myfun ≠≠ myfun myfun

A C program may contain several functionsA C program may contain several functions Must have a main()Must have a main() The main( ) is where execution of your program beginsThe main( ) is where execution of your program begins The main( ) is when your program begins runningThe main( ) is when your program begins running Executing statements are included inside Executing statements are included inside ‘‘{{’’ and and ‘‘}}’’ All C statements end with a semicolonAll C statements end with a semicolon(;)(;) You may place two or more statements on one lineYou may place two or more statements on one line

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 27

C program StructureC program Structure

1.1. Understand the components Understand the components of a C programof a C program

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 28

Example of C program StructureExample of C program Structure

1.1. Understand the components Understand the components of a C programof a C program

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 29

1.1. Understand the components Understand the components of a C programof a C program

ANSIANSI   C standard specifies a set of library functions to be supplied C standard specifies a set of library functions to be supplied by all C compilers, which your program may useby all C compilers, which your program may use

C standard libraryC standard library I/O (input/output) functionI/O (input/output) function String manipulation functionString manipulation function Mathematical compugation functionMathematical compugation function Etc.Etc.

header fileheader file File extension : File extension : .h .h #include#include  preprocessor directivepreprocessor directive

C ignores spaces C ignores spaces

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 30

1.1. Understand the components Understand the components of a C programof a C program

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 31

2. Create and compile a program2. Create and compile a program

Turbo C++Turbo C++ Program-development environmentsProgram-development environments Development stepsDevelopment steps

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 32

2. Create and compile a program2. Create and compile a program

A direction of compile processingA direction of compile processing

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 33

3. Declare variables and 3. Declare variables and assign valuesassign values

VariableVariable A named memory locationA named memory location Can hold various valuesCan hold various values

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 34

3. Declare variables and 3. Declare variables and assign valuesassign values

Variable declarationVariable declaration Form : Form : type var-nametype var-name

type : C data type type : C data type var-name : name of the variablevar-name : name of the variable

Declaration places of variable Declaration places of variable Global variable : outside all functionsGlobal variable : outside all functions Local variable : inside a functionLocal variable : inside a function

The same type variables declarationThe same type variables declaration

int count ;int count ;

float x;float y;float z;

float x;float y;float z;

float x, y, z ;float x, y, z ;

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 35

3. Declare variables and 3. Declare variables and assign valuesassign values

Assignment operationAssignment operation Form : Form : vaiable-name = value;vaiable-name = value;

Ex) num = 100;Ex) num = 100;

ConstantConstant Fixed valueFixed valueEx) Ex) ‘‘AA’’, , ‘‘aa’’, 100, 100.0, 100, 100.0

Display of characters, integers and floating-point valuesDisplay of characters, integers and floating-point values

Format specifiers of printf()Format specifiers of printf() %d%d(decimal format)(decimal format), %c, %c(character value)(character value), %f, %f(float, double)(float, double)

printf(“This prints the number %d”, 99);printf(“This prints the number %d”, 99);

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 36

3. Declare variables and 3. Declare variables and assign valuesassign values

Example 1.Example 1. Example 2.Example 2.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 37

4. Input numbers from the keyboard4. Input numbers from the keyboard

Standard input/outputStandard input/output

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 38

4. Input numbers from the keyboard4. Input numbers from the keyboard

scanf()scanf() Standard function for input numbers from the keyboardStandard function for input numbers from the keyboard Format : Format : scanf( scanf(““%d%d””, &int-var-name);, &int-var-name);

Ex) int num;Ex) int num; scanf(scanf(““%d%d””, &num);, &num);

Format : Format : scanf( scanf(““%f%f””, &float-var-name);, &float-var-name);

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 39

4. Input numbers from the keyboard4. Input numbers from the keyboard

Input number from the keyboardInput number from the keyboard

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 40

4. Input numbers from the keyboard4. Input numbers from the keyboard

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 41

4. Input numbers from the keyboard4. Input numbers from the keyboard

ExerciseExercise Write a program that inputs two floating-point Write a program that inputs two floating-point

numbers numbers (use type float)(use type float) and then displays their sum. and then displays their sum.

*** Addition program ***

First num : 15

Second num : 20

15 + 20 = 35

*** Addition program ***

First num : 15

Second num : 20

15 + 20 = 35

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 42

5. Perform calculations using arithmetic 5. Perform calculations using arithmetic expressionsexpressions

Arithmetic operatorsArithmetic operators + : addition+ : addition - : subtraction- : subtraction * : multiplication* : multiplication / : division/ : division % : modulus% : modulus

ex)ex)

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 43

5. Perform calculations using arithmetic 5. Perform calculations using arithmetic expressionsexpressions

Exercise #1Exercise #1 Write a program that computes the volume of a cube. Have the Write a program that computes the volume of a cube. Have the

program prompt the user for each dimension.program prompt the user for each dimension.

Input width : 15Input length : 10Input height : 12The volume of a cube : 1800

Input width : 15Input length : 10Input height : 12The volume of a cube : 1800

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 44

5. Perform calculations using arithmetic 5. Perform calculations using arithmetic expressionsexpressions

Exercise #2Exercise #2 Write a program that computes the number of Write a program that computes the number of

seconds in a year.seconds in a year.

The seconds of a year : 31536000 sec The seconds of a year : 31536000 sec

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 45

6. Add comments to a program6. Add comments to a program

CommentsComments A note to yourself that you put into your source code.A note to yourself that you put into your source code. All comments are ignored by the compiler.All comments are ignored by the compiler.

/* This is a comment. */ /* This is a comment. */ /* /*                                            This is a long comment.This is a long comment. Me too! Me too!                                 */*/

int i ;int i ;     //this is daclaration of variable//this is daclaration of variable

/* comment start*/ comment end// only this line after it

/* comment start*/ comment end// only this line after it

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 46

6. Add comments to a program6. Add comments to a program

nested comments problemnested comments problem

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 47

6. Add comments to a program6. Add comments to a program

ExampleExample A year on Jupiter A year on Jupiter (the time takes for Jupiter to make one full (the time takes for Jupiter to make one full

circuit around the Sun) circuit around the Sun) takes about 12 Earth years. The takes about 12 Earth years. The following program allows you to convert Earth days to Jovian following program allows you to convert Earth days to Jovian years.years.

Simply specify the number of Earth days, and it computes the Simply specify the number of Earth days, and it computes the equivalent number of Jovian years. Notice the use of comments equivalent number of Jovian years. Notice the use of comments throughout the program.throughout the program.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 48

7. Write your own functions7. Write your own functions

Functions are the building blocks of C.Functions are the building blocks of C.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 49

7. Write your own functions7. Write your own functions

Most real-world programs will contain many functions.Most real-world programs will contain many functions. ret-typeret-type specifies the type of data returned by the function. specifies the type of data returned by the function.

void : function does not return a valuevoid : function does not return a value A function prototypeA function prototype declares a function before it is used and prior to declares a function before it is used and prior to

its definition.its definition. FunctionFunction’’s names name Return typeReturn type Parameter listParameter list

The compiler needs to know this information in order for it to properly execute a call to the function.

The compiler needs to know this information in order for it to properly execute a call to the function.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 50

7. Write your own functions7. Write your own functions

Example 1Example 1

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 51

7. Write your own functions7. Write your own functions

Execution order of Example 1Execution order of Example 1

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 52

7. Write your own functions7. Write your own functions

Example 2Example 2

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 53

8. Use functions to return values8. Use functions to return values

A function may return a value to the calling routine.A function may return a value to the calling routine.

#include <stdio.h>  #include <math.h>  /* needed by sqrt() */     int main(void) {           double answer;

          answer = sqrt(10.0);   // sqrt() call assign to answer          printf ("%f", answer);

          return 0;         }

#include <stdio.h>  #include <math.h>  /* needed by sqrt() */     int main(void) {           double answer;

          answer = sqrt(10.0);   // sqrt() call assign to answer          printf ("%f", answer);

          return 0;         }

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 54

8. Use functions to return values8. Use functions to return values

In functions, you can return a value to the calling routine using the In functions, you can return a value to the calling routine using the returnreturn statement. statement.

• form :  return value ;

                (constant or variable or expression)

  int func(void) {       return 10;   /* return 10 to calling routine. */ }

  int func(void) {       return 10;   /* return 10 to calling routine. */ }

  int func(void) ; int main(void) {       int num;

num = func(); /* func() call receive a value(10). */ printf(“%d”, num);

return 0;   }

  int func(void) ; int main(void) {       int num;

num = func(); /* func() call receive a value(10). */ printf(“%d”, num);

return 0;   }

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 55

8. Use functions to return values8. Use functions to return values

Example 1Example 1

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 56

8. Use functions to return values8. Use functions to return values

Example 2Example 2

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 57

8. Use functions to return values8. Use functions to return values

ExerciseExercise Write a program that uses a function called convert(), which Write a program that uses a function called convert(), which

prompts the user for an amount in dollars and returns this value prompts the user for an amount in dollars and returns this value converted into pounds. converted into pounds. (Use an exchange rate of $2.00 per (Use an exchange rate of $2.00 per pound.)pound.) Display the conversion. Display the conversion.

Input dollars : 1515.00 dollars are 7.50 pounds.

Input dollars : 1515.00 dollars are 7.50 pounds.

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 58

9. Use function arguments9. Use function arguments

A A functionfunction’’s argumentss arguments is a value that is passed to the is a value that is passed to the function when the function is called.function when the function is called.

The The formal parameterformal parameter is special variables to receive is special variables to receive argument values.argument values.

void sum(int x, int y) {

printf(“%d ”, x + y);

}

void sum(int x, int y) {

printf(“%d ”, x + y);

}

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 59

9. Use function arguments9. Use function arguments

#include <stdio.h>         int sum(int x, int y);

        int main(void) {           sum(1, 20);    /* 1 and 20 are arguments to function */           sum(9, 6);           sum(81, 9);

          return 0;         }

        int sum(int x, int y) {   /* x and y are parameters of function */           printf ("%d\n ", x + y); return 0;        }

#include <stdio.h>         int sum(int x, int y);

        int main(void) {           sum(1, 20);    /* 1 and 20 are arguments to function */           sum(9, 6);           sum(81, 9);

          return 0;         }

        int sum(int x, int y) {   /* x and y are parameters of function */           printf ("%d\n ", x + y); return 0;        }

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 60

9. Use function arguments9. Use function arguments

Example 1Example 1

Example 2Example 2 This program uses the out char() function to output characters to the This program uses the out char() function to output characters to the

screen.screen.

sum(10-2, 9*7);sum(10-2, 9*7); sum(8, 63);sum(8, 63);

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 61

9. Use function arguments9. Use function arguments

ExcercisesExcercises Write program that uses a function called Write program that uses a function called outnum()outnum()

that takes one integer argument and displays it on the that takes one integer argument and displays it on the screen.screen.

Input integer number : 43Your input number is 43

Input integer number : 43Your input number is 43

MELJUN CORTES, MELJUN CORTES, MBA,MPA,BSCSMBA,MPA,BSCS

School of Computer InformationSchool of Computer Information 62

10. Remember the C keywords10. Remember the C keywords

autobreakcasecharconstcontinuedefaultdo

doubleelseenumexternfloatforgotoif

intlongregisterreturnshortsignedsizeofstatic

structswitchtypedefunionunsignedvoidvolatilewhile

asm_ssinterrupt

_cscdeclnear

_dsfarpascal

_eshuge

KeywordsKeywords

Extended KeywordsExtended Keywords