Top Banner
Introduction to Programming Engr. Rashid Farid Chishti [email protected] Chapter 05: Functions International Islamic University H-10, Islamabad, Pakistan http://www.iiu.edu.pk
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Introduction to ProgrammingEngr. Rashid Farid [email protected] 05: FunctionsInternational Islamic University H-10, Islamabad, Pakistanhttp://www.ii.ed.pkFunctionsFunctionsAA function function groups a number of programgroups a number of program statements into a unit and gives it a name. statements into a unit and gives it a name.This unit can then beThis unit can then be invoked invoked from other partsfrom other parts of the program. of the program.The most important reason to use functions isThe most important reason to use functions is to aid in theto aid in the conceptual organization conceptual organization of aof a program programAnother reason to use functions is toAnother reason to use functions is to reducereduce program size program size. Any sequence of instructions. Any sequence of instructions that appears in a program more than once is athat appears in a program more than once is a candidate for being made into a function. candidate for being made into a function.The functions code is stored inThe functions code is stored in only oneonly one place place in memory, even though the function isin memory, even though the function is executed many times executed many times in the course of thein the course of the program. program.Return typenput argumentsFunctions!!demonstrates a simple function"include #iostream$using namespace std%int cube&int x'%!! function derationint main&'( !! tests the cube&' function)int n * +%,hile &n -* .'(cin $$ n%cout## /0tcube&/ ## n ## /' * 1## cube&n' ## endl%!! 2alling a function3 !! end of ,hile loopsystem&/4A567/'% return .%3!!end of mainint cube& int x '(!! function definitionreturn x8x8x%!! returns cube of x)3!! ( function body 3nput ArgumentsReturn typeFunctionsFunctions7ach integer read is passed to the7ach integer read is passed to the cube&' cube&' function by thefunction by the callcall cube&n' cube&n'. The. The value returned value returned by the function replacesby the function replaces the expression cube&n' and then is passed to the outputthe expression cube&n' and then is passed to the output ob9ectob9ect cout coutTheThe main&' main&' function passes the value : to thefunction passes the value : to the cube&' cube&' function, and thefunction, and the cube&' cube&' functionfunction returns the value returns the value +;:+;: to theto the main&' main&' function. function.TheThe argument n argument n isis passed by value passed by value to the formalto the formal parameterparameter x x. This simply means that. This simply means that x is assigned the value of n x is assigned the value of n ,hen the function is,hen the function is called called. .Two Argument Functions!!2alculates max of t,o