SlideShare a Scribd company logo
FUNCTIONS & POINTERS
Instructor
Dhiviya Rose J , AP-Sr. Scale | SoCSE
WAProgram ????? output
HELLO I AM A FUNCTION NAMED HELLO
WELCOME I AM A FUNCTION NAMED WELCOME
HELLO I AM A FUNCTION NAMED HELLO
HELLO I AM A FUNCTION NAMED HELLO
WELCOME I AM A FUNCTION NAMED WELCOME
HELLO I AM A FUNCTION NAMED HELLO
Need/Advantages of function
• Modular Programming
• Makes C program readable
• Enhance application software process
• Program development becomes easy
• Reusability
• Module once created can be used any no of time
Definition – Function
• encapsulates a subset of a program
• To hide details
• To be invoked from multiple places
• To share with others
• A fragment of code
• accepts zero or more argument values
• produces a result value, and has zero or more side effects.
Functions
• Types of Functions in C
• Function definition
• Function prototypes & Header files
• Pre- and post-conditions
• Scope and storage class
• Implementation of functions
• Recursive functions
TYPES OF FUNCTIONS
Arguments???? -> input to functions
CSEG1001 Unit 4 Functions and Pointers
Common Predefined Functions in C
#include <math.h>
• sin(x) // radians
• cos(x) // radians
• tan(x) // radians
• atan(x)
• atan2(y,x)
• exp(x) // ex
• log(x) // loge x
• log10(x) // log10 x
• sqrt(x) // x  0
• pow(x, y) // xy
• ...
#include <stdio.h>
• printf()
• fprintf()
• scanf()
• sscanf()
• ...
#include <string.h>
• strcpy()
• strcat()
• strcmp()
• strlen()
• ...
Function Definition
resultType functionName(type1 arg1, type2 arg2, …)
{
…
body
…
}
• If no result, resultType should be void
• If no parameters, use void between ()
Example
Definitions
• Argument:–
• an expression passed when a function is called
• int a;
• a=pow(2,3);
• 2 and 3 are the arguments
Function call of pow()
• Returns type = int
• Argument = 2 integers
Examples of function definition
• int pow(int a,int b)
{
int ans,i;
ans=1;
for(i=1;i<=b;i++)
ans=ans*a;
}
// pow function
Function Prototypes
• a function must be used separate from where it is defined
–
• before its definition in the same C program
• Therefore, we need some way to declare a function
separate from defining its body.
• Called a Function Prototype
Function Prototypes (continued)
• Definition:– a Function Prototype in C is a language
construct of the form:–
return-type function-name (parameter declarations) ;
POINTERS IN C
Pointers
• A pointer is a reference to another variable in a program
• Stores the memory location
Pointer Variable Definition
• Basic syntax:
datatype *ptrName
• Can declare pointers to any data type
• Examples:
int *P; /* P is var that can point to an int var */
float *Q; /* Q is a float pointer */
char *R; /* R is a char pointer */
• Complex example:
int *AP[5]; /* AP is an array of 5 pointers to ints *
Pointer Variable Initialization - Address Operator
• The address (&) operator points the memory location of
the variable in memory
Syntax: &variablename
Examples:
int a=10;
int *b;
b=&a;
Pointer variable ‘b’
holding the
address of variable
‘a’
Dereferencing Pointers - Indirection (*) Operator
• Is to get the value of the variable it refers to
• Syntax: *PointerVariable
Example:
int age = 19;
int *P = &V;
printf(“%d”,*P);
/* Prints 19 */
Go through …..
* and & are inverses
Cancel each other out
*&myVar == myVar
and
&*yPtr == yPtr
Knowledge Check
Assume address of A as 2002 and B
as 4005
int A = 3;
int B = 4;
int *P = &A;
int *Q = &B;
printf(“%d %dn”,A,B);
printf(“%d %dn”,*P,*Q);
printf(“%p %pn”,P,Q);
Pointers to Pointers
• A pointer can also be made to point to a pointer variable
Example:
int V = 101;
int *P = &V; /* P points to int V */
int **Q = &P; /* Q points to int pointer P */
printf(“%d %d %dn”,V,*P,**Q); /* prints 101 3 times */
Pointer Arithmetic
• Increment/decrement pointer (++ or --)
• Add an integer to/from a pointer( + or += )
• Pointers may be subtracted from each other (- or -=)
• Pointer arithmetic is performed on an array
Pointer Arithmetic on Arrays
• 5 element int array on a machine using 4 byte ints
• vPtr points to first element v[ 0 ], which is at location 3000
• vPtr = 3000
• vPtr += 2; sets vPtr to 3008
• vPtr points to v[ 2 ]
pointer variable
vPtr
v[0] v[1] v[2] v[4]v[3]
3000 3004 3008 3012 3016
location
Characters and Strings with Pointers
• String assignment
• Character array:
char color[] = "blue";
• Creates 5 element char array, color, (last element is '0')
• variable of type char *
char *colorPtr = "blue";
• Creates a pointer to string “blue”, colorPtr, and stores it somewhere in
memory
Arrays of Pointers
• Arrays can contain pointers
• Commonly used to store an array of strings
char *suit[ 4 ] = {"Hearts", "Diamonds",
"Clubs", "Spades" };
• Each element of suit is a pointer to a char * (a
string)
suit[3]
suit[2]
suit[1]
suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’0’
’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’0’
’C’ ’l’ ’u’ ’b’ ’s’ ’0’
’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’0’
Knowledge Check
#include<stdio.h>
int main()
{
int a = 10;
void *p = &a;
int *ptr = p;
printf("%u",*ptr);
return 0;
}
(A) 10
(B) Address
(C) 2
(D) Compilation error
(E) None of above
Knowledge Check
(A) Address, Address, 3
(B) Address, 3, 3
(C) 3, 3, 3
(D) Compilation error
(E) None of above
#include<stdio.h>
int main(){
int i = 3;
int *j;
int **k;
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
return 0;
}
Knowledge Check
Knowledge Check
#include<stdio.h>
int main()
{
int *ptr, b;
b = sizeof(ptr);
printf("%d" , b);
return 0;
}
CSEG1001 Unit 4 Functions and Pointers

More Related Content

PDF
CSEG1001Unit 3 Arrays and Strings
PDF
CSEG1001 Unit 5 Structure and Unions
PPTX
Templates in C++
PDF
PDF
Thinking in Functions: Functional Programming in Python
PPTX
PPT
CPP Language Basics - Reference
PPT
Scala functions
CSEG1001Unit 3 Arrays and Strings
CSEG1001 Unit 5 Structure and Unions
Templates in C++
Thinking in Functions: Functional Programming in Python
CPP Language Basics - Reference
Scala functions

What's hot (20)

PPT
Function overloading(C++)
PPT
Functions in C++
PDF
Scala categorytheory
PDF
Functional Python Webinar from October 22nd, 2014
PDF
CS4200 2019 | Lecture 2 | syntax-definition
ODP
Clojure basics
PPTX
Templates presentation
PPTX
Library functions in c++
PPT
Pointers C programming
PPT
PPTX
Function C++
PPTX
An Introduction to Functional Programming with Javascript
PPT
Loops and functions in r
PDF
Pointers
PPT
Unit 6 pointers
PDF
Generic programming and concepts that should be in C++
PDF
Functional programing in Javascript (lite intro)
PDF
Implicit conversion and parameters
DOC
Functions struct&union
Function overloading(C++)
Functions in C++
Scala categorytheory
Functional Python Webinar from October 22nd, 2014
CS4200 2019 | Lecture 2 | syntax-definition
Clojure basics
Templates presentation
Library functions in c++
Pointers C programming
Function C++
An Introduction to Functional Programming with Javascript
Loops and functions in r
Pointers
Unit 6 pointers
Generic programming and concepts that should be in C++
Functional programing in Javascript (lite intro)
Implicit conversion and parameters
Functions struct&union
Ad

Similar to CSEG1001 Unit 4 Functions and Pointers (20)

PPT
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
PPTX
pointers in c programming - example programs
PDF
4th unit full
PPTX
Functions IN CPROGRAMMING OF ENGINEERING.pptx
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
PPTX
2-Concept of Pointers in c programming.pptx
PPT
U19CS101 - PPS Unit 4 PPT (1).ppt
PPTX
Unit No 2.pptx Basic s of C Programming
PPT
Lap trinh C co ban va nang cao
PPTX
Chp3(pointers ref)
PPT
Lecture2.ppt
PPT
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
16717 functions in C++
 
PPT
pointer, structure ,union and intro to file handling
PPTX
Introduction to C++
PPSX
Pointers
PPTX
Functions in C.pptx
PPT
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
pointer, structure ,union and intro to file handling
PPT
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
pointers in c programming - example programs
4th unit full
Functions IN CPROGRAMMING OF ENGINEERING.pptx
UNIT 4 POINTERS.pptx pointers pptx for basic c language
2-Concept of Pointers in c programming.pptx
U19CS101 - PPS Unit 4 PPT (1).ppt
Unit No 2.pptx Basic s of C Programming
Lap trinh C co ban va nang cao
Chp3(pointers ref)
Lecture2.ppt
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
16717 functions in C++
 
pointer, structure ,union and intro to file handling
Introduction to C++
Pointers
Functions in C.pptx
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Ad

More from Dhiviya Rose (14)

PDF
Programming for Problem Solving Unit 2
PDF
Programming for Problem Solving Unit 1
PPTX
Module 3 microsoft powerpoint
PPT
Module 3 business computing.pdf
PDF
Module 2 Digital Devices and its Applications
PDF
Software
PDF
Unit 1 Business Computing
PDF
Module 1 - Digital Devices and its Application
PDF
CSEG1001Unit 2 C Programming Fundamentals
PPTX
CSEG1001 Lecture 1 Introduction to Computers
PDF
Lecture 3 internet and web
PPSX
Strings
PPSX
Multidimentional array
PPSX
Searching in Arrays
Programming for Problem Solving Unit 2
Programming for Problem Solving Unit 1
Module 3 microsoft powerpoint
Module 3 business computing.pdf
Module 2 Digital Devices and its Applications
Software
Unit 1 Business Computing
Module 1 - Digital Devices and its Application
CSEG1001Unit 2 C Programming Fundamentals
CSEG1001 Lecture 1 Introduction to Computers
Lecture 3 internet and web
Strings
Multidimentional array
Searching in Arrays

Recently uploaded (20)

PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
IGGE1 Understanding the Self1234567891011
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
Introduction to Building Materials
PPTX
Computer Architecture Input Output Memory.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Hazard Identification & Risk Assessment .pdf
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
My India Quiz Book_20210205121199924.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Weekly quiz Compilation Jan -July 25.pdf
IGGE1 Understanding the Self1234567891011
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Introduction to Building Materials
Computer Architecture Input Output Memory.pptx
Computing-Curriculum for Schools in Ghana
LDMMIA Reiki Yoga Finals Review Spring Summer
Unit 4 Computer Architecture Multicore Processor.pptx
Hazard Identification & Risk Assessment .pdf
Practical Manual AGRO-233 Principles and Practices of Natural Farming
A powerpoint presentation on the Revised K-10 Science Shaping Paper
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Introduction to pro and eukaryotes and differences.pptx
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
FORM 1 BIOLOGY MIND MAPS and their schemes
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
My India Quiz Book_20210205121199924.pdf

CSEG1001 Unit 4 Functions and Pointers

  • 1. FUNCTIONS & POINTERS Instructor Dhiviya Rose J , AP-Sr. Scale | SoCSE
  • 2. WAProgram ????? output HELLO I AM A FUNCTION NAMED HELLO WELCOME I AM A FUNCTION NAMED WELCOME HELLO I AM A FUNCTION NAMED HELLO HELLO I AM A FUNCTION NAMED HELLO WELCOME I AM A FUNCTION NAMED WELCOME HELLO I AM A FUNCTION NAMED HELLO
  • 3. Need/Advantages of function • Modular Programming • Makes C program readable • Enhance application software process • Program development becomes easy • Reusability • Module once created can be used any no of time
  • 4. Definition – Function • encapsulates a subset of a program • To hide details • To be invoked from multiple places • To share with others • A fragment of code • accepts zero or more argument values • produces a result value, and has zero or more side effects.
  • 5. Functions • Types of Functions in C • Function definition • Function prototypes & Header files • Pre- and post-conditions • Scope and storage class • Implementation of functions • Recursive functions
  • 7. Arguments???? -> input to functions
  • 9. Common Predefined Functions in C #include <math.h> • sin(x) // radians • cos(x) // radians • tan(x) // radians • atan(x) • atan2(y,x) • exp(x) // ex • log(x) // loge x • log10(x) // log10 x • sqrt(x) // x  0 • pow(x, y) // xy • ... #include <stdio.h> • printf() • fprintf() • scanf() • sscanf() • ... #include <string.h> • strcpy() • strcat() • strcmp() • strlen() • ...
  • 10. Function Definition resultType functionName(type1 arg1, type2 arg2, …) { … body … } • If no result, resultType should be void • If no parameters, use void between ()
  • 12. Definitions • Argument:– • an expression passed when a function is called • int a; • a=pow(2,3); • 2 and 3 are the arguments Function call of pow() • Returns type = int • Argument = 2 integers
  • 13. Examples of function definition • int pow(int a,int b) { int ans,i; ans=1; for(i=1;i<=b;i++) ans=ans*a; } // pow function
  • 14. Function Prototypes • a function must be used separate from where it is defined – • before its definition in the same C program • Therefore, we need some way to declare a function separate from defining its body. • Called a Function Prototype
  • 15. Function Prototypes (continued) • Definition:– a Function Prototype in C is a language construct of the form:– return-type function-name (parameter declarations) ;
  • 17. Pointers • A pointer is a reference to another variable in a program • Stores the memory location
  • 18. Pointer Variable Definition • Basic syntax: datatype *ptrName • Can declare pointers to any data type • Examples: int *P; /* P is var that can point to an int var */ float *Q; /* Q is a float pointer */ char *R; /* R is a char pointer */ • Complex example: int *AP[5]; /* AP is an array of 5 pointers to ints *
  • 19. Pointer Variable Initialization - Address Operator • The address (&) operator points the memory location of the variable in memory Syntax: &variablename Examples: int a=10; int *b; b=&a; Pointer variable ‘b’ holding the address of variable ‘a’
  • 20. Dereferencing Pointers - Indirection (*) Operator • Is to get the value of the variable it refers to • Syntax: *PointerVariable Example: int age = 19; int *P = &V; printf(“%d”,*P); /* Prints 19 */
  • 21. Go through ….. * and & are inverses Cancel each other out *&myVar == myVar and &*yPtr == yPtr
  • 22. Knowledge Check Assume address of A as 2002 and B as 4005 int A = 3; int B = 4; int *P = &A; int *Q = &B; printf(“%d %dn”,A,B); printf(“%d %dn”,*P,*Q); printf(“%p %pn”,P,Q);
  • 23. Pointers to Pointers • A pointer can also be made to point to a pointer variable Example: int V = 101; int *P = &V; /* P points to int V */ int **Q = &P; /* Q points to int pointer P */ printf(“%d %d %dn”,V,*P,**Q); /* prints 101 3 times */
  • 24. Pointer Arithmetic • Increment/decrement pointer (++ or --) • Add an integer to/from a pointer( + or += ) • Pointers may be subtracted from each other (- or -=) • Pointer arithmetic is performed on an array
  • 25. Pointer Arithmetic on Arrays • 5 element int array on a machine using 4 byte ints • vPtr points to first element v[ 0 ], which is at location 3000 • vPtr = 3000 • vPtr += 2; sets vPtr to 3008 • vPtr points to v[ 2 ] pointer variable vPtr v[0] v[1] v[2] v[4]v[3] 3000 3004 3008 3012 3016 location
  • 26. Characters and Strings with Pointers • String assignment • Character array: char color[] = "blue"; • Creates 5 element char array, color, (last element is '0') • variable of type char * char *colorPtr = "blue"; • Creates a pointer to string “blue”, colorPtr, and stores it somewhere in memory
  • 27. Arrays of Pointers • Arrays can contain pointers • Commonly used to store an array of strings char *suit[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" }; • Each element of suit is a pointer to a char * (a string) suit[3] suit[2] suit[1] suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’0’ ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’0’ ’C’ ’l’ ’u’ ’b’ ’s’ ’0’ ’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’0’
  • 28. Knowledge Check #include<stdio.h> int main() { int a = 10; void *p = &a; int *ptr = p; printf("%u",*ptr); return 0; } (A) 10 (B) Address (C) 2 (D) Compilation error (E) None of above
  • 29. Knowledge Check (A) Address, Address, 3 (B) Address, 3, 3 (C) 3, 3, 3 (D) Compilation error (E) None of above #include<stdio.h> int main(){ int i = 3; int *j; int **k; j=&i; k=&j; printf("%u %u %d ",k,*k,**k); return 0; }
  • 31. Knowledge Check #include<stdio.h> int main() { int *ptr, b; b = sizeof(ptr); printf("%d" , b); return 0; }