0% found this document useful (0 votes)
11 views3 pages

Third

Computer Programming

Uploaded by

abdullahabd7op
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Third

Computer Programming

Uploaded by

abdullahabd7op
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab – Computer Programming (CE-231)

Lab 4: Functions in C++


Basic Concepts

 Function Definition: A block of code that performs a specific task. Functions provide
code reusability.
 Function Declaration: The prototype of the function that specifies the function's
name, return type, and parameters.
 Passing Values: Data can be passed to a function through parameters (either by value
or by reference).

Program 1: Function to Add Two Numbers


#include<iostream>
using namespace std;

int add(int a, int b) { // Function definition


return a + b;
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum: " << add(num1, num2); // Function call
return 0;
}
Output:
Variables Execution 1 Execution 2 Execution 3
num1
num2
Output of function

12 DEPARTMENT OF CIVIL ENGINEERING BZU MULTAN


Lab – Computer Programming (CE-231)

Program 2: Function to Find Maximum of Two Numbers


#include<iostream>
using namespace std;

int findMax(int a, int b) {


if(a > b)
return a;
else
return b;
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Maximum: " << findMax(num1, num2);
return 0;
}
Output:
Variables Execution 1 Execution 2 Execution 3
num1
num2
Output of function

Program 3: Understanding the concept of function inside a function.


# include <stdio.h>
void italy( ) ;
void brazil( ) ;
void argentina( ) ;
int main( )
{
printf ( "I am in main\n" ) ;
italy( ) ;
printf ( "I am finally back in main\n" ) ;
return 0 ;
}
void italy( )
{
printf ( "I am in italy\n" ) ;
brazil( ) ;
printf ( "I am back in italy\n" ) ;
}
void brazil( )
{
printf ( "I am in brazil\n" ) ;
argentina( ) ;
}
void argentina( )
{
printf ( "I am in argentina\n" ) ;
}

Output:

13 DEPARTMENT OF CIVIL ENGINEERING BZU MULTAN


Lab – Computer Programming (CE-231)

Code Fragments for Output Prediction


# include <stdio.h>
int check ( int ) ;
int main( )
{
int i = 15, c ;
c = check ( i ) ;
cout << c;
cout << “I am Tricking you!”;
return 0 ;
}
int check ( int ch )
{
if ( ch >= 45 )
return ( 100 ) ;
else
return ( 6 * 10 ) ;
}
#include <stdio.h>
int main( )
{
void slogan( ) ;
int c = 5 ;
c = slogan( ) ;
cout<< c;
return 0 ;
}
void slogan( )
{
cout<<"Only men use C!\n";
}

Practice Problems

1. Write a function to calculate the factorial value of any integer entered through the
keyboard.
2. Write a function power ( a, b ), to calculate the value of a raised to b.
3. Write a function that receives marks received by a student in 3 subjects and returns
the average and percentage of these marks. Call this function from main ( ) and print
the results in main ( )

14 DEPARTMENT OF CIVIL ENGINEERING BZU MULTAN

You might also like