Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
39 views

Basic Programming Lab

This document discusses functions in C programming. It begins by explaining the syntax for defining functions, including return types and parameters. It then provides examples of different types of functions: library functions, user-defined functions, and main. It categorizes functions based on whether they have arguments and return values. Examples are given for each category. The document also discusses recursion with an example of a function to calculate the sum of natural numbers recursively. Finally, it provides some programming assignments involving writing functions and using recursion.

Uploaded by

IsSid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Basic Programming Lab

This document discusses functions in C programming. It begins by explaining the syntax for defining functions, including return types and parameters. It then provides examples of different types of functions: library functions, user-defined functions, and main. It categorizes functions based on whether they have arguments and return values. Examples are given for each category. The document also discusses recursion with an example of a function to calculate the sum of natural numbers recursively. Finally, it provides some programming assignments involving writing functions and using recursion.

Uploaded by

IsSid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Basic Programming Lab

— — 0x10
Function Declaration
Syntax:
return_data_type function_name (parameters)
{
//code
}

// Program to print hello world using function

#include <stdio.h>

void Greet();

int main()
{
Greet();
return 0;
}

void Greet()
{
printf("\nHello World");
}
Function Types
1 - Library Functions

- printf
- scanf
- sqrt

2 - User Defined Functions

- main
Category of Function

1- With no arguments and no return value

#include <stdio.h>
void Sum_Of_Two_Nos_1(void);

int main()
{
Sum_Of_Two_Nos_1();
return 0;
}

void Sum_Of_Two_Nos_1()
{
int Number_1, Number_2, Result;
printf("\nEnter First Number: ");
scanf("%d", &Number_1);
printf("\nEnter Second Number: ");
scanf("%d", &Number_2);
Result = Number_1 + Number_2;
printf("\nFirst Number + Second Number = %d\n",Result);
}
2- With arguments but no return value
#include <stdio.h>

void Sum_Of_Two_Nos_2(int Number_1, int Number_2);

int main()
{
int Number_1, Number_2;
printf("\nEnter First Number: ");
scanf("%d", &Number_1);
printf("\nEnter Second Number: ");
scanf("%d", &Number_2);
Sum_Of_Two_Nos_2(Number_1, Number_2);
return 0;
}

void Sum_Of_Two_Nos_2(Number_1, Number_2)


{
int Result;
Result = Number_1 + Number_2;
printf("\nFirst Number + Second Number = %d\n",Result);
}
3- With no arguments but a return value
#include <stdio.h>

int Sum_Of_Two_Nos_3();

int main()
{
int Result;
Result = Sum_Of_Two_Nos_3();
printf("\nFirst Number + Second Number = %d\n",Result);
return 0;
}

int Sum_Of_Two_Nos_3()
{
int Number_1, Number_2;
printf("\nEnter First Number: ");
scanf("%d", &Number_1);
printf("\nEnter Second Number: ");
scanf("%d", &Number_2);
return Number_1 + Number_2;
}
4- With arguments and return value
#include <stdio.h>

int Sum_Of_Two_Nos_4(int Number_1, int Number_2);

int main()
{
int Number_1, Number_2;
int Result;
printf("\nEnter First Number: ");
scanf("%d", &Number_1);
printf("\nEnter Second Number: ");
scanf("%d", &Number_2);
Result = Sum_Of_Two_Nos_4(Number_1, Number_2);
printf("\nFirst Number + Second Number = %d\n",Result);
return 0;
}

int Sum_Of_Two_Nos_4(Number_1, Number_2)


{
return Number_1 + Number_2;
}
Recursion
// Program to add first n natural numbers

#include<stdio.h>
int Sum_of_Natural_Numbers(int Number);

int main()
{
int Number, Result;

printf("Enter a Number: ");


scanf("%d", &Number);

Result = Sum_of_Natural_Numbers(Number);

printf("Sum upto given no = %d", Result);


return 0;
}

int Sum_of_Natural_Numbers(int Number)


{
if (Number != 0)
{
return Number + Sum_of_Natural_Numbers(Number - 1);
}
else
{
return Number;
}
}
Assignment
//0x09

//Use scanf for input in Every Program


//Do not Use In-Built Functions

1. Write a function to make a calculator.(Category 1)

2. Write a function to swap two numbers. (Category 2)

3. Write a menu based program using function to


perform following operations

a) Read and Print a Complex No


b) Add two Complex Nos
c) Subtract two Complex Nos
d) Multiply two Complex Nos

4. Write a program to find the factorial of a no


using recursion.

5. Write a program to print fibonacci series upto n


using recursion.

6. Continuation with Assignment 7 Question 2. Write a


program to check whether the triangle can be formed
or not, if yes check whether the triangle is
equilateral or not. Take 3 coordinates as vertices of
Triangle. If the triangle can be formed then
calculate the area of the triangle.

a) function to calculate sides


b) function to calculate area
Points to Remember
1. Filetype: .c

2. Naming Convention for File: Question_Y.c


where Y = Question No in that Assignment
example: Question_1.c

3. Write your details in every program

/*
--------------------------------------------------
|Author : Your_Name |
|Roll No: Your_Roll_No |
|Department: Your_Department |
--------------------------------------------------
*/

You might also like