

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!
Exmaple D:
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