Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Functions: Types, Declaration, and Examples, Exercises of Computer Fundamentals

An overview of functions in c++, including their general form, parts, different forms, and examples. It covers function declaration, void functions, functions with arguments and return values, and library functions such as pow and sqrt.

Typology: Exercises

2011/2012

Uploaded on 07/03/2012

prakash
prakash 🇮🇳

4.6

(10)

63 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computing Fundamental
Lab-07
ThegeneralformofafunctiondefinitioninC++isasfollows:
return type function-name ( parameter-list )
{
local variable-definitions;
function-implementation;
return value;
}
Parts of functions.
1- Type of the value that the function will return
2- Name of the function
3- Zero or more variables that becomes the input to the
functions
4- Body of the function, contains the code which is
executed when the function is called
5- The values that function wants to return
Key Note: When a function don’t want to return a value, it
tells the compiler by means of void,
There can be following forms of a functions
A. Function with no return values and no arguments
B. Functions with arguments with no return value
C. Functions with return value with no arguments
D. Functions with return value and arguments
Example A:
void DisplayMyName(void){
cout<<”My Name is Ahmed”<<endl;
}
Key Note: void in the function means that function will not
return any value
Example B:
void EvenOrOdd(int num){
if(num%2==0)
cout<<”Even”<endl;
else
cout<<”Odd”<<endl;
}
Example C:
float DollaRate(void){
return 85.6;
}
Exmaple D:
int makeEven(int x){
int result;
if(x%2==0)
result=x;
else
result=x+1;
return result;
}
Declaring and Using a function
A function should be declared before main(), one way is to
declare and define the function before main, other way is to
declare the function before main and define it after main
Example(Method 1):
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int add(int x,int y){
int result;
result=x+y;
return result;
}
main(){
clrscr();
int a;
a=add(10,20);
cout<<”Sum of 10 and 20 is”<<a<<endl;
}
1 2 3
4
5
Docsity.com
pf2

Partial preview of the text

Download C++ Functions: Types, Declaration, and Examples and more Exercises Computer Fundamentals in PDF only on Docsity!

Computing Fundamental

Lab-

The general form of a function definition in C++ is as follows:

return type function-name ( parameter-list )

local variable-definitions ;

function-implementation ;

return value;

Parts of functions.

1- Type of the value that the function will return

2- Name of the function

3- Zero or more variables that becomes the input to the

functions

4- Body of the function, contains the code which is

executed when the function is called

5- The values that function wants to return

Key Note : When a function don’t want to return a value, it

tells the compiler by means of void,

There can be following forms of a functions

A. Function with no return values and no arguments

B. Functions with arguments with no return value

C. Functions with return value with no arguments

D. Functions with return value and arguments

Example A:

void DisplayMyName(void){

cout<<”My Name is Ahmed”<<endl;

Key Note : void in the function means that function will not

return any value

Example B:

void EvenOrOdd(int num){

if(num%2==0)

cout<<”Even”<endl;

else

cout<<”Odd”<<endl;

Example C:

float DollaRate(void){

return 85.6;

Exmaple D:

int makeEven(int x){

int result;

if(x%2==0)

result=x;

else

result=x+1;

return result;

Declaring and Using a function A function should be declared before main(), one way is to declare and define the function before main, other way is to declare the function before main and define it after main

Example(Method 1) : #include<stdio.h> #include<conio.h> #include<iostream.h>

int add(int x,int y){ int result; result=x+y; return result; } main(){ clrscr(); int a; a=add(10,20); cout<<”Sum of 10 and 20 is”<<a<<endl; }

Docsity.com

Example(Method 2) : #include<stdio.h> #include<conio.h> #include<iostream.h>

int add(int ,int ); main(){ clrscr(); int a; a=add(10,20); cout<<”Sum of 10 and 20 is”<<a<<endl; }

int add(int x,int y){ int result; result=x+y; return result; } Some Library Functions

pow(x,y): returns x y^ ; sqrt(x): returns the square root of variable x

Note : Both of these functions require math.h to be included in the program

Example:

#include<stdio.h> #include<conio.h> #include<iostream.h> #include<math.h>

main(){ clrscr(); int a; cout<<”Enter value of a : “; cin>>a; cout<<”Squre root of “<<a<<” is “<<sqrt(x);

}

Example: #include<stdio.h> #include<conio.h> #include<iostream.h> #include<math.h> main(){ clrscr(); int a,b; cout<<”Enter value of a : “; cin>>a; cout<<”Enter power of “<<a <<endl;

cin>>b; cout<<”The “<<b<<”th power of “<<a <<”is “<<endl; cout<<pow(a,b);

Task 1:

Write following functions and write programs to execute these functions

1-Add: A functions that takes two arguments and returns the sum of the two

2-Mul: A functions that takes two arguments and returns the product of the two

More Functions

#include<stdio.h> #include<conio.h> #include<iostream.h>

void PositiveDivisors(int );

main(){ clrscr(); int a; cout<<”Enter a positive integer”<<endl; cin>>a; cout<<”The positive divisors of “<<a <<”are”<<endl; PositiveDivisors(a); } void PositiveDivisors(int x){

for(int a=1;a<=x;a++){ if(a%2==0) cout<<a<<endl;

}

Tasks: 1- Write a function that displays your biodata 2- Write a function that displays even integer between two numbers A and B 3- Write a function that returns the greater of the two numbers

Docsity.com