Functions-Computer Fundamentals-Lecture Slides, Slides of Computer Fundamentals

Following points have been discussed in these lecture slides by Baljit Ashvin at Indian Institute of Information Technology (IIIT) under subject of Computer Fundamentals: Functions, Prototype, Definition, Generic, Structure, Body, Main, Return, Type, Arguments

Typology: Slides

2011/2012

Uploaded on 07/03/2012

mehr5
mehr5 🇮🇳

4.4

(8)

36 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Introduction to Computing
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Functions-Computer Fundamentals-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

Introduction to Computing

Functions

  • Revision
    • Function Prototype
    • Function Definition Generic structure of function

return type function_name (function arguments) {

Function body

return (optional) value;

}

Function Definition

  • Is prototype is sufficient?
    • No a prototype is only one pieces of information
    • Actual code is written for the function which is called the definition of the function
  • Where do we place the definition of the function?
    • One place for function definition is the space after main function
  • Can we not place function definition before main?
    • A function can be defined before main in that case prototype is not required
  • Why two different styles of writing user functions?
    • Some programmer like main to be at the top
    • Some like user functions to be on the top

Example-

----header files ---

void aboutMe(void);

void main(void){

cout<<“Hello”; aboutMe();

}

void aboutMe(void){

cout<<“My name is Alpha”<<endl; cout<<“Address: Islamabad”<<endl; cout<<“Cell: 123456789”<<endl;

}

Prototype

Definition

Output Hello My name is Alpha Address :Islamabad Cell:

Function Return Type

  • What is the meaning of return and return type
    • A function can return a value at the place where it was called
    • A function must have to declare what type of value it will return
    • A function must have to use return keyword to return a value
  • A function can return any data type (primitive)
    • int, float, char, double, long int...

int myFunction1(void); the function should return an integer float myFunction2(void); the function should return a float double myFunction3(void); the function should return a double long int myFunction4(void); the function should return a long integer

Function return type

Example ---header files--- float height(void); int weight(void);

void main(void){ cout<<“Hello my name is Alpha”<<endl; cout<<“My weight is ”<<weight()<<endl; cout<<“My height is ”<<height()<<endl; }

int height(void){ return 5.6; }

int weight(void){ int w=55; return w; }

Output Hello my name is Alpha My weight is 55 My height is 5.

Function Arguments

  • Analogy
    • In mathematics f(x) represents a function that operates on all values of x
    • In Y =f(x) Y represents value of the function when operated on x
    • f(x) = x^2 is a function that operates on x and it squares value of x
    • What will be the values of Y in following cases
    • Y=f(2) f(x) = x^2
    • Y=f(3) f(x) = x+
    • Y=f(4) f(x) = x^2 +x
    • Y=f(5) f(x) = 2x + 3
    • Y=f(6) f(x) = 3x+ x^2
  • Some function can operate on two or more independent

variables

  • Y =f(a,b) where f(a,b)=a2+b2 Y=
  • Y =f(a,b,c) where f(a,b,c)=a+2b+c Y=

Function Arguments

  • Like Mathematics , in programming functions can be passed

arguments

  • A function has to declare the type and number of arguments

that can be passed to it

Example

int add(int , int); two arguments of type integer can be passed int mul(int,int); same int remainder(int,int); same float divide(float,int); two arguments , first float and second integer can be passed

Example

---header files--- int add(int,int);

void main(void ){ int result; cout<<“The sum of 5 and 10 is =”<<add(5,10)<<endl; result=add(40,50); cout<<“The sum of 40 and 50 is ”<<result<<endl; }

int add(int a, int b){ int sum; sum=a+b; return sum }

Output The sum of 5 and 10 is 15 The sum of 40 and 50 is 90

About functions

  • Can we declare variables in functions
    • Yes
  • Can we use loops in functions
    • Yes
  • Can we make functions in functions
    • No
  • Can we call a function in another function
    • Yes
  • Can we pass less values than number of arguments of a

function

  • No , compiler error
  • Can we change the order of values when passing to a function
  • No

Function Call

  • Where does all variables and functions stored in memory?
    • When a program is executed , a special memory structure called stack stores values and function calls
  • Each function has its own area in stack
    • The arguments of functions are also stored on stack
  • Can one function use other functions variables
    • No
  • Is it possible to share variables between functions
    • Yes
    • Such variables are placed outside function on memory area that is shared by all functions

Function Call

---definitions of all functions-----

main(){

function1(); function2();

function3(); function4();

function5();

}

A when a function is called it is placed on stack , after function finishes its

work it is removed from the stack.

What is the life of variables in the function?

main() var1,var

function1() var3,var function2() Var5,var function3() var7,var

function4() var9,var

function5() Var11,var

Global vs Local variables