Understanding Functions: Definition, Declaration, Calling, and Returning Values, Exercises of Computer Programming

The concept of functions, their importance in programming, and how to define, declare, call, and return values from functions using c++ as an example. It also includes exercises for practicing these concepts.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya 🇮🇳

5

(4)

32 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
FUNCTIONS
A function groups a number of program
statements into a unit and gives it a name. This
unit can then be invoked from other parts of the
program.
The most important reason to use functions is
to aid in the conceptual organization of a
program.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Functions: Definition, Declaration, Calling, and Returning Values and more Exercises Computer Programming in PDF only on Docsity!

FUNCTIONS

A function groups a number of program

statements into a unit and gives it a name. This

unit can then be invoked from other parts of the

program.

The most important reason to use functions is

to aid in the conceptual organization of a

program.

FUNCTIONS

Another reason to use functions (and the

reason they were invented, long ago) is to

reduce program size. Any sequence of

instructions that appears in a program more

than once is a candidate for being made into a

function. The function’s code is stored in only

one place in memory, even though the function

is executed many times in the course of the

program.

#include //for cout, etc. void star_line(void); using namespace std; int main() { star_line(); cout<<”Wellcome to My Program”<<endl; star_line(); return 0; } void star_line(void) { for(int i = 0; i < 45 ; i++) cout<<”*”; cout<<endl; }

FUNCTIONS

Function contains three parts

The Function Declaration

Just as you can’t use a variable without first

telling the compiler what it is, you also can’t use

a function without telling the compiler about it.

 e.g void star_line(void);

Calling the Function

 all we need to call the function is the function

name, followed by parentheses and a

semicolon.

 e.g star_line();

FUNCTIONS

Void my_func(void);

Int my_func(void);

Void my_func(int , float);

Float my_func(int , char);

#include //for cout, etc. void star_line(char , int); using namespace std; int main() { star_line('-',45); cout<<”Wellcome to My Program”<<endl; star_line('*',23); return 0; } void star_line(char ch , int count) { for(int i = 0; i < count ; i++) cout<<ch; cout<<endl; }

RETURNING a Value

When a function completes its execution, it can

return a single value to the calling program.

Usually this return value consists of an answer

to the problem the function has solved. The

next example demonstrates a function that

returns a weight in kilograms after being given

a weight in pounds.

#include //for cout, etc. float lbs_to_kg(float); using namespace std; int main() { float lbs , kg; cout<<”Enter your weight in pounds : ”; cin>>lbs; kg = lbs_to_kg(lbs); cout<<”Your Weight in Kilograms is : “<<kg<<endl; return 0; } float lbs_to_kg(float pounds) { int kilogram; kilogram = 0.453592 * pounds; return kilogram; } docsity.com

LAB TASK 1

Write a function called circarea() that finds the

area of a circle in a similar way. It should take

an argument of type float and return an

argument of the same type. Write a main()

function that gets a radius value from the user,

calls circarea(), and displays the result.

LAB TASK 2

Raising a number n to a power p is the same as

multiplying n by itself p times. Write a function

called power() that takes a double value for n

and an int value for p, and returns the result as

a double value.

LAB TASK 4

Write a function called hms_to_secs() that

takes three int values—for hours, minutes, and

seconds—as arguments, and returns the

equivalent time in seconds (type long). Create a

program that exercises this function by

repeatedly obtaining a time value in hours,

minutes, and seconds from the user (format

12:59:59), calling the function, and displaying

the value of seconds it returns.