Functions
, Function
•Aprogram segment that carries out some specific,
well-defined task
•Example
◦ A function to add two numbers
◦ A function to find the largest of n numbers
•A function will carry out its intended task whenever
it is called or invoked
◦ Can be called multiple times
, Why Functions?
•Allows one to develop a program in a modular
fashion
◦ Divide-and-conquer approach
◦ Construct a program from small pieces or
components
•Useexisting functions as building blocks for new
programs
•Abstraction: hide internal details (library functions)
3
, Note
•Every C program consists of one or more
functions
•One of these functions must be called main
•Executionof the program always begins by
carrying out the instructions in main
•Functions call other functions as instructions
, Function
•Aprogram segment that carries out some specific,
well-defined task
•Example
◦ A function to add two numbers
◦ A function to find the largest of n numbers
•A function will carry out its intended task whenever
it is called or invoked
◦ Can be called multiple times
, Why Functions?
•Allows one to develop a program in a modular
fashion
◦ Divide-and-conquer approach
◦ Construct a program from small pieces or
components
•Useexisting functions as building blocks for new
programs
•Abstraction: hide internal details (library functions)
3
, Note
•Every C program consists of one or more
functions
•One of these functions must be called main
•Executionof the program always begins by
carrying out the instructions in main
•Functions call other functions as instructions