0% found this document useful (0 votes)
10 views

pic function notes

A function in programming is a self-contained block of code designed to perform a specific task, promoting modularity, reusability, and easier debugging. Functions can be categorized into library functions, which are built-in, and user-defined functions, which are created by the programmer. Key concepts include function definitions, declarations, calls, variable scope, parameters, return types, and recursion.

Uploaded by

patilmohit882
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)
10 views

pic function notes

A function in programming is a self-contained block of code designed to perform a specific task, promoting modularity, reusability, and easier debugging. Functions can be categorized into library functions, which are built-in, and user-defined functions, which are created by the programmer. Key concepts include function definitions, declarations, calls, variable scope, parameters, return types, and recursion.

Uploaded by

patilmohit882
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/ 9

Function

Function :- A function is a self content block of code or sub programs with a set of
statements that perform some specific task or coherent task when it is called as function.

Advantages of Using Functions :-

(1) Modular code

(2) Reusability

(3) Ease of debugging

(4) Better code organization

Need of function :-

1. Avoid repetition:Write code once, use it many times.


2. Make code cleaner and easier to understand: It breaks big problems into small
parts.
3. Easy to find and fix errors:Debugging is easier when code is organized.
4. Reuse code:Same function can be used in many places.

Types of functions :-

(1) Library functions :- In C, library functions are built-in functions that are
provided by the C Standard Library. These functions are pre-written and
compiled, so you can use them in your program without writing them from
scratch.
(2) User defined function :- In C, a user-defined function is a function that you
create yourself to perform a specific task. It helps organize code, make it
reusable, and improve readability.

Library functions :-
(1) Math function <Math.h> :- In C, math functions are a set of functions used to perform
common mathematical operations like square roots, trigonometric calculations,
powers, logarithms, etc. These functions are defined in the math.h header file.

Function Description Example


Sqrt(x) Square root Sqrt(16) = 4.0
Pow(x, y) X raised to the Pow(2.0, 3.0)= 8.0
power y
Sin(x) Sine (x in radians) Sin(M_PI/2) =1.0
Floor(x) Round down Floor(4.8)→ 4.0
Ceil(x) Rounds up Ceil(4.2)→ 5.0
log10(x) Base-10 logarithm Log10(1000)= 3.0

cos(x) Cosine Cos(0) -> 1

Example:-

#include <stdio.h>

#include <math.h>

Int main() {

Double x = 9.0;

Double result = sqrt(x);

Printf(“Square root of %.2f is %.2f\n”, x, result);

Return 0;

String handling function<string.h> :- In C, string handling functions are used to perform


operations on strings like copying, comparing, concatenating, finding length, and searching
within strings. These functions are defined in the string.h header file.

Functions Description Example


Strlen(s) Returns the length Strlen(“hello “) → 5
of string s
Strcpy(dest, src) Copies src into Strcpy(dest, “hi”)
dest
Strstr(s1, s2) Find first Strstr(“hello
occurrence of s2 world”, “world”)
in s1
Strcmp(s1, s2) Compares two Strcmp(“a”, “b”)→
strings (0 if equal) negative
Strcat(dest, src) Appends src to the strcat(dest,
end of dest "world")
Strcpy(src,dest) Copy src to the Strcpy(dest)= src
dest

getchar() :- It read one character from the keywords .

Example :-

Char ch;

Ch = getchar();

Putchar() :- It reads one character on the screen.

Example :-

Char ch = ‘A’;

Putchar(ch);

Malloc() (memory allocation) :- it gives you empty space in memory (dynamic memory).

Example :-

Int *ptr;

Ptr = (int *) malloc(5 * sizeof(int));

Calloc() (continuous allocation) :- Like malloc (), but it also sets memory to 0.

Example :-

int *ptr;

Ptr = (int *) calloc(5, sizeof(int));


Writing use defined function :-

Function definition:- function definition consist of the world description and code of the
function. It tells about what function is doing what are it's inputs and what are its output. It
consist of two parts of function header and function body.

Syntax:-

return type function (type1 arg1 type3 arg2 type3 arg3)

Local variable declaration;

Statement 1;

Statement 2;

Return value

Function declaration :- function declaration is also know as function prototype. Eat in form
the compiler about three thing, those name of the function, number and type of argument
received by the function and the type of value returned by the function.

Function call :- A function call when you tell the computer to run a function.

Example :-

#include <stdio.h>

Int add(int a, int b) { Return a + b; }

Int main() {

Int result = add(3, 4); // This is a function call

Printf(“The answer is: %d\n”, result);

Return 0;

}
Scope of variable :-

Scope Local variable Global variable


Declared Inside a function Outside of all
or block functions
Scope Only accessible Accessible from
within that any function in the
function program.
Lifetime Created when Exist for the entry
the function is duration of the
called, destroy program
when the
function ends
Memory Stack Data segment
location (global area)
Definition A local variable Global variable
declare inside a declare outside
function; the function;
accessible only accessible
within that through the
function. program
Syntax Void datatype
function_name(){ variable_name;
datatype Void
variable_name; function_name(){
} }

Function parameters :-

Call by value and call by reference :-

Parameter Call by value Call by reference


Definition A copy of the actual values That address of the variable
is passed to the function is passed to the function
Changes made Do not affect the original Do affect the original value
value
Used with Normal variable passing Pointer
Syntax Void change(int x){ Void change(int *x){
X=10; *X=10;
} }
Passed Value Address
Function return value :- A function can return value using written statement. The value it's
return must match function return type.

Function return type :- The function return type define the type of value that a function will
return to its caller. It's specified before the function name its declaration and definition.

Declaring function written types :-

1] integer return type

Example :-

Int add(int a, int b); // Declaration (usually at the top or in a

header file)

Int add(int a, int b) { // Definition

Return a + b;

2] void return type

Example :-

Void sayHello();

Void sayHello() {

Printf(“Hello!\n”);

3] floating point return type

Example :-

Float getAverage(float x, float y) {

Return (x + y) / 2.0;

4] pointer return type

Example :-

Int* getPointerToValue() {

Static int value = 10;


Return &value;

Recursive function :-A recursive function in C is a function that calls itself to solve a
problem. It’s commonly used for problems that can be broken down into smaller sub-
problems of the same type.

Example :-

#include <stdio.h>

Int factorial(int n) {

If (n == 0) {

Return 1;

} else {

Return n * factorial(n – 1);

Int main() {

Int number = 5;

Printf(“Factorial of %d is %d\n”, number, factorial(number));

Return 0;

Category of function based on argument and return type :-

1] function with no argument & no return value :-

Syntax :-

Void function(void);

Main()

{
Void function()

Statement;

2] function with no argument but return value :-

Syntax :-

Int function,(void);

Main()

Int r;

r=fun();

Int fun()

return(exp);

3] function with argument but no return value :-

Syntax :-

Void function (int,int);

Main()

Int (a,b);

Void function(int x,int y);

Statement;
}

4] function with argument and return value :-

Syntax :-

Fun(int,int);

Main()

Int r=function(a,b);

Int fun(int x,int y)

return (exp);

You might also like